Skip to content

Commit 6a15c62

Browse files
committed
add one edit
1 parent eb0f268 commit 6a15c62

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Strings/one_edit.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
func OneEdit(stringOne string, stringTwo string) bool {
4+
lengthOne := len(stringOne)
5+
lengthTwo := len(stringTwo)
6+
if abs(lengthOne-lengthTwo) > 1 {
7+
return false
8+
}
9+
for i := 0; i < min(lengthOne, lengthTwo); i++ {
10+
if stringOne[i] != stringTwo[i] {
11+
if lengthOne > lengthTwo {
12+
return stringOne[i+1:] == stringTwo[i:]
13+
} else if lengthTwo > lengthOne {
14+
return stringTwo[i+1:] == stringOne[i:]
15+
} else {
16+
return stringOne[i+1:] == stringTwo[i+1:]
17+
}
18+
}
19+
}
20+
return true
21+
}
22+
23+
func abs(a int) int {
24+
if a < 0 {
25+
return -a
26+
}
27+
return a
28+
}
29+
func min(a, b int) int {
30+
if a < b {
31+
return a
32+
}
33+
return b
34+
}

0 commit comments

Comments
 (0)