Skip to content

Commit 49a1032

Browse files
committed
added problem 859
1 parent 5782e8d commit 49a1032

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

problems/problem0859/buddy_strings.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package problem0859
2+
3+
/*
4+
Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.
5+
Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].
6+
For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
7+
*/
8+
9+
func buddyStrings(s string, goal string) bool {
10+
// Different length - can't swap to fix
11+
if len(s) != len(goal) {
12+
return false
13+
}
14+
if s == goal {
15+
// If they're both the same, we can only make them equal
16+
// by swapping duplicate letters twice
17+
var set uint32 // Save existence of letters as bits
18+
for i := range s {
19+
if set&(1<<(s[i]-'a')) > 0 {
20+
// If we've already seen this letter, there are duplicates
21+
return true
22+
}
23+
// Mark letter as seen
24+
set |= (1 << (s[i] - 'a'))
25+
}
26+
return false
27+
}
28+
// Dif is an array of indexes where the strings differ
29+
var dif = make([]int, 0, len(s))
30+
for i := 0; i < len(s) && len(dif) <= 2; i++ {
31+
if s[i] != goal[i] {
32+
dif = append(dif, i)
33+
}
34+
}
35+
return len(dif) == 2 && // We can only fix two differences
36+
s[dif[0]] == goal[dif[1]] && // the differences should be the same letter
37+
s[dif[1]] == goal[dif[0]] // and should be symmetrical
38+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problem0859
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type TestCase struct {
11+
S, Goal string
12+
Expected bool
13+
}
14+
15+
var TestCases = []TestCase{
16+
{"ab", "ba", true},
17+
{"ab", "ab", false},
18+
{"aa", "aa", true},
19+
}
20+
21+
func TestBuddyStrings(t *testing.T) {
22+
assert := assert.New(t)
23+
24+
for _, tc := range TestCases {
25+
want := tc.Expected
26+
got := buddyStrings(tc.S, tc.Goal)
27+
assert.Equal(want, got, fmt.Sprintf("%+v", tc))
28+
}
29+
}
30+
31+
func BenchmarkBuddyStrings(b *testing.B) {
32+
for i := 0; i < b.N; i++ {
33+
for _, tc := range TestCases {
34+
buddyStrings(tc.S, tc.Goal)
35+
}
36+
}
37+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ Each problem is in it's own directory, with test files. There are helper package
295295
| 0844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [My Solution](./problems/problem0844) ||
296296
| 0848 | [Shifting Letters](https://leetcode.com/problems/shifting-letters) | [My Solution](./problems/problem0848) ||
297297
| 0858 | [Mirror Reflection](https://leetcode.com/problems/mirror-reflection) | [My Solution](./problems/problem0858) ||
298+
| 0859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings) | [My Solution](./problems/problem0859) ||
298299
| 0864 | [Shortest Path to Get All Keys](https://leetcode.com/problems/shortest-path-to-get-all-keys) | [My Solution](./problems/problem0864) ||
299300
| 0867 | [Transpose Matrix](https://leetcode.com/problems/transpose-matrix) | [My Solution](./problems/problem0867) ||
300301
| 0869 | [Reordered Power of 2](https://leetcode.com/problems/reordered-power-of-2) | [My Solution](./problems/problem0869) ||

0 commit comments

Comments
 (0)