-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path854-KSimilarStrings.go
More file actions
77 lines (69 loc) · 2.64 KB
/
854-KSimilarStrings.go
File metadata and controls
77 lines (69 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
// 854. K-Similar Strings
// Strings s1 and s2 are k-similar (for some non-negative integer k) if we can swap the positions of two letters in s1 exactly k times so that the resulting string equals s2.
// Given two anagrams s1 and s2, return the smallest k for which s1 and s2 are k-similar.
// Example 1:
// Input: s1 = "ab", s2 = "ba"
// Output: 1
// Explanation: The two string are 1-similar because we can use one swap to change s1 to s2: "ab" --> "ba".
// Example 2:
// Input: s1 = "abc", s2 = "bca"
// Output: 2
// Explanation: The two strings are 2-similar because we can use two swaps to change s1 to s2: "abc" --> "bac" --> "bca".
// Constraints:
// 1 <= s1.length <= 20
// s2.length == s1.length
// s1 and s2 contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}.
// s2 is an anagram of s1.
import "fmt"
func kSimilarity(a string, b string) int {
res, achars, bchars := 0, []byte(a), []byte(b)
checkAllOptions := func(achars []byte, bchars []byte, i int, b string) int {
res := 1 << 31 - 1
for j := i + 1; j < len(achars); j++ {
if achars[j] == bchars[i] && achars[j] != bchars[j] {
achars[i], achars[j] = achars[j], achars[i]
newStr := string(achars)
res = min(res, 1 + kSimilarity(newStr, b))
achars[i], achars[j] = achars[j], achars[i] // backtrack
}
}
return res
}
getAllPerfectMatches := func(achars []byte, bchars []byte) int {
res := 0
for i := 0; i < len(achars); i++ {
if achars[i] == bchars[i] {
continue
}
for j := i + 1; j < len(achars); j++ {
if achars[j] == bchars[i] && bchars[j] == achars[i] {
achars[i], achars[j] = achars[j], achars[i]
res++
break
}
}
}
return res
}
res += getAllPerfectMatches(achars, bchars)
for i := 0; i < len(achars); i++ {
if achars[i] == bchars[i] {
continue
}
return res + checkAllOptions(achars, bchars, i, b)
}
return res
}
func main() {
// Example 1:
// Input: s1 = "ab", s2 = "ba"
// Output: 1
// Explanation: The two string are 1-similar because we can use one swap to change s1 to s2: "ab" --> "ba".
fmt.Println(kSimilarity("ab", "ba")) // 1
// Example 2:
// Input: s1 = "abc", s2 = "bca"
// Output: 2
// Explanation: The two strings are 2-similar because we can use two swaps to change s1 to s2: "abc" --> "bac" --> "bca".
fmt.Println(kSimilarity("abc", "bca")) // 2
}