Skip to content

Commit c1e261e

Browse files
authored
Merge pull request #1597 from akgmage/dev
add oneedit in python
2 parents 4760a3e + 9d573ba commit c1e261e

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

Strings/one_edit.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
You're given two strings stringone and stringtwo. Write a function that determines if these
2+
You're given two strings stringone and stringtwo. Write a function that determines if these
33
two strings can be made equal using only one edit.
44
5-
5+
66
There are 3 possible edits:
77
Replace: One character in one string is swapped for a different character.
88
Add:: One character is added at any index in one string.
@@ -13,33 +13,35 @@
1313
1414
1515
Explanation:
16-
The code snippet is implementing the "One Edit Away" algorithm, which determines whether two given
16+
The code snippet is implementing the "One Edit Away" algorithm, which determines whether two given
1717
strings are one edit away from each other. An edit is defined as either inserting a character, removing a character, or replacing a character.
1818
19-
The `OneEdit` function takes two strings as input and returns a boolean indicating whether
19+
The `OneEdit` function takes two strings as input and returns a boolean indicating whether
2020
they are one edit away. Here's the breakdown of the algorithm:
2121
2222
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
23+
2. Check if the difference in lengths is greater than 1. If so, return `false` because it's
2424
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
25+
3. Traverse both strings until the shorter one is fully traversed or an unequal character is
2626
found.
27-
4. If an unequal character is found, check the remaining portion of the strings to determine
27+
4. If an unequal character is found, check the remaining portion of the strings to determine
2828
if they are still one edit away.
29-
5. Check the remaining characters in the longer string compared to the remaining characters
29+
5. Check the remaining characters in the longer string compared to the remaining characters
3030
in the shorter string.
3131
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
32+
7. If the loop completes without finding any unequal characters, the strings are either
3333
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
34+
8. The `abs` and `min` functions are utility functions used to calculate the absolute value
3535
and minimum of two integers, respectively.
3636
3737
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.
3838
3939
O(n) time | O(1) space - where n is the length of the shorter string
40-
41-
40+
41+
4242
*/
43+
package main
44+
4345
func OneEdit(stringOne string, stringTwo string) bool {
4446
lengthOne := len(stringOne)
4547
lengthTwo := len(stringTwo)

Strings/one_edit.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
def OneEdit(stringOne, stringTwo):
43+
lengthOne = len(stringOne)
44+
lengthTwo = len(stringTwo)
45+
46+
# Check the difference in lengths between the two strings.
47+
# If the difference is greater than 1, it is not possible to make one edit to make them equal.
48+
if abs(lengthOne - lengthTwo) > 1:
49+
return False
50+
51+
# Traverse the strings until the shorter one is fully traversed or an unequal character is found.
52+
for i in range(min(lengthOne, lengthTwo)):
53+
# If an unequal character is found, check the remaining portion of the strings to determine if they are still one edit away.
54+
if stringOne[i] != stringTwo[i]:
55+
# Check the remaining characters in the longer string compared to the remaining characters in the shorter string.
56+
# Return True if they match, indicating they are one edit away.
57+
if lengthOne > lengthTwo:
58+
return stringOne[i+1:] == stringTwo[i:]
59+
elif lengthTwo > lengthOne:
60+
return stringTwo[i+1:] == stringOne[i:]
61+
else:
62+
return stringOne[i+1:] == stringTwo[i+1:]
63+
64+
# If the loop completes without finding any unequal characters, the strings are either identical or differ only in length by 1.
65+
return True
66+
67+
def abs(a):
68+
if a < 0:
69+
return -a
70+
return a
71+
72+
def min(a, b):
73+
if a < b:
74+
return a
75+
return b

0 commit comments

Comments
 (0)