Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c1735b

Browse files
author
Sandy
authored
Merge pull request #111 from openset/develop
Add: buddy_strings
2 parents 6f5d6c6 + f2a8bca commit 9c1735b

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

‎problems/buddy-strings/buddy_strings.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
11
package buddy_strings
2+
3+
func buddyStrings(A string, B string) bool {
4+
if len(A) != len(B) {
5+
return false
6+
}
7+
if A == B {
8+
exist := [26]bool{}
9+
for _, c := range A {
10+
k := c - 'a'
11+
if exist[k] {
12+
return true
13+
}
14+
exist[k] = true
15+
}
16+
return false
17+
} else {
18+
m, n := -1, -1
19+
for i, c := range A {
20+
if B[i] != byte(c) {
21+
if m == -1 {
22+
m = i
23+
} else if n == -1 {
24+
n = i
25+
} else {
26+
return false
27+
}
28+
}
29+
}
30+
return n != -1 && A[m] == B[n] && A[n] == B[m]
31+
}
32+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
11
package buddy_strings
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
a string
7+
b string
8+
expected bool
9+
}
10+
11+
func TestBuddyStrings(t *testing.T) {
12+
tests := [...]caseType{
13+
{
14+
a: "ab",
15+
b: "ba",
16+
expected: true,
17+
},
18+
{
19+
a: "aa",
20+
b: "aa",
21+
expected: true,
22+
},
23+
{
24+
a: "ab",
25+
b: "ab",
26+
expected: false,
27+
},
28+
{
29+
a: "aaaaaaabc",
30+
b: "aaaaaaacb",
31+
expected: true,
32+
},
33+
{
34+
a: "",
35+
b: "aa",
36+
expected: false,
37+
},
38+
{
39+
a: "hello",
40+
b: "h0lle",
41+
expected: false,
42+
},
43+
{
44+
a: "hello",
45+
b: "hanna",
46+
expected: false,
47+
},
48+
}
49+
for _, tc := range tests {
50+
output := buddyStrings(tc.a, tc.b)
51+
if output != tc.expected {
52+
t.Fatalf("input: %v %v, output: %v, expected: %v", tc.a, tc.b, output, tc.expected)
53+
}
54+
}
55+
}

‎problems/reorder-log-files/reorder_log_files.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ func reorderLogFiles(logs []string) []string {
1111
s1 := strings.SplitN(logs[i], " ", 2)
1212
s2 := strings.SplitN(logs[j], " ", 2)
1313
f1, f2 := "0"+s1[1], "0"+s2[1]
14-
if unicode.IsNumber(rune(f1[1])) {
14+
if unicode.IsDigit(rune(f1[1])) {
1515
f1 = "1"
1616
}
17-
if unicode.IsNumber(rune(f2[1])) {
17+
if unicode.IsDigit(rune(f2[1])) {
1818
f2 = "1"
1919
}
2020
return f1 < f2

‎problems/string-to-integer-atoi/string_to_integer_atoi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func myAtoi(str string) int {
1515
str = str[1:]
1616
}
1717
i := strings.IndexFunc(str, func(r rune) bool {
18-
return !unicode.IsNumber(r)
18+
return !unicode.IsDigit(r)
1919
})
2020
if i > -1 {
2121
str = str[0:i]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /