Skip to content

Commit 4760a3e

Browse files
authored
Merge pull request #1596 from akgmage/dev
Dev
2 parents 99c4372 + 3f3ad07 commit 4760a3e

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

Strings/one_edit.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
You're given two strings stringone and stringtwo. Write a function that determines if these
3+
two strings can be made equal using only one edit.
4+
5+
6+
There are 3 possible edits:
7+
Replace: One character in one string is swapped for a different character.
8+
Add:: One character is added at any index in one string.
9+
Remove: One character is removed at any index in one string.
10+
11+
Sample Input: StringOne: alaska StringTwo: aloska
12+
Output: True
13+
14+
15+
Explanation:
16+
The code snippet is implementing the "One Edit Away" algorithm, which determines whether two given
17+
strings are one edit away from each other. An edit is defined as either inserting a character, removing a character, or replacing a character.
18+
19+
The `OneEdit` function takes two strings as input and returns a boolean indicating whether
20+
they are one edit away. Here's the breakdown of the algorithm:
21+
22+
1. Calculate the lengths of the two strings.
23+
2. Check if the difference in lengths is greater than 1. If so, return `false` because it's
24+
not possible to make one edit to make the strings equal.
25+
3. Traverse both strings until the shorter one is fully traversed or an unequal character is
26+
found.
27+
4. If an unequal character is found, check the remaining portion of the strings to determine
28+
if they are still one edit away.
29+
5. Check the remaining characters in the longer string compared to the remaining characters
30+
in the shorter string.
31+
6. Return `true` if the remaining portions match, indicating they are one edit away.
32+
7. If the loop completes without finding any unequal characters, the strings are either
33+
identical or differ only in length by 1, which means they are one edit away.
34+
8. The `abs` and `min` functions are utility functions used to calculate the absolute value
35+
and minimum of two integers, respectively.
36+
37+
The algorithm efficiently checks for the possibility of one edit by comparing the characters at corresponding indices and handling cases where the lengths of the strings are different.
38+
39+
O(n) time | O(1) space - where n is the length of the shorter string
40+
41+
42+
*/
43+
func OneEdit(stringOne string, stringTwo string) bool {
44+
lengthOne := len(stringOne)
45+
lengthTwo := len(stringTwo)
46+
47+
// Check the difference in lengths between the two strings.
48+
// If the difference is greater than 1, it is not possible to make one edit to make them equal.
49+
if abs(lengthOne - lengthTwo) > 1 {
50+
return false
51+
}
52+
53+
// Traverse the strings until the shorter one is fully traversed or an unequal character is found.
54+
for i := 0; i < min(lengthOne, lengthTwo); i++ {
55+
// If an unequal character is found, check the remaining portion of the strings to determine if they are still one edit away.
56+
if stringOne[i] != stringTwo[i] {
57+
// Check the remaining characters in the longer string compared to the remaining characters in the shorter string.
58+
// Return true if they match, indicating they are one edit away.
59+
if lengthOne > lengthTwo {
60+
return stringOne[i+1:] == stringTwo[i:]
61+
} else if lengthTwo > lengthOne {
62+
return stringTwo[i+1:] == stringOne[i:]
63+
} else {
64+
return stringOne[i+1:] == stringTwo[i+1:]
65+
}
66+
}
67+
}
68+
69+
// If the loop completes without finding any unequal characters, the strings are either identical or differ only in length by 1.
70+
return true
71+
}
72+
73+
func abs(a int) int {
74+
if a < 0 {
75+
return -a
76+
}
77+
return a
78+
}
79+
80+
func min(a, b int) int {
81+
if a < b {
82+
return a
83+
}
84+
return b
85+
}

0 commit comments

Comments
 (0)