Skip to content

Commit 7d53ae7

Browse files
committed
Update str diff solution
Update constraints, test cases, and dictionary solution. Fix inconsistencies between challenge and solution notebooks.
1 parent 95d0d26 commit 7d53ae7

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

arrays_strings/str_diff/str_diff_challenge.ipynb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
"source": [
3535
"## Constraints\n",
3636
"\n",
37-
"* Assume two strings str1, str2, where str2 contains the same set of characters in str1 with one additional character.\n",
3837
"* Can we assume the strings are ASCII?\n",
3938
" * Yes\n",
4039
"* Is case important?\n",
4140
" * The strings are lower case\n",
4241
"* Can we assume the inputs are valid?\n",
4342
" * No, check for None\n",
43+
" * Otherwise, assume there is only a single different char between the two strings\n",
4444
"* Can we assume this fits memory?\n",
4545
" * Yes"
4646
]
@@ -52,6 +52,8 @@
5252
"## Test Cases\n",
5353
"\n",
5454
"* None input -> TypeError\n",
55+
"* 'ab', 'aab' -> 'a'\n",
56+
"* 'aab', 'ab' -> 'a'\n",
5557
"* 'abcd', 'abcde' -> 'e'\n",
5658
"* 'aaabbcdd', 'abdbacade' -> 'e'"
5759
]
@@ -82,7 +84,7 @@
8284
"source": [
8385
"class Solution(object):\n",
8486
"\n",
85-
" def find_diff(self, s, t):\n",
87+
" def find_diff(self, str1, str2):\n",
8688
" # TODO: Implement me\n",
8789
" pass"
8890
]
@@ -117,9 +119,15 @@
117119
"\n",
118120
" def test_find_diff(self):\n",
119121
" solution = Solution()\n",
120-
" assert_raises(TypeError, solution.find_diff, None, None)\n",
122+
" assert_raises(TypeError, solution.find_diff, None)\n",
123+
" assert_equal(solution.find_diff('ab', 'aab'), 'a')\n",
124+
" assert_equal(solution.find_diff('aab', 'ab'), 'a')\n",
121125
" assert_equal(solution.find_diff('abcd', 'abcde'), 'e')\n",
122126
" assert_equal(solution.find_diff('aaabbcdd', 'abdbacade'), 'e')\n",
127+
" assert_equal(solution.find_diff_xor('ab', 'aab'), 'a')\n",
128+
" assert_equal(solution.find_diff_xor('aab', 'ab'), 'a')\n",
129+
" assert_equal(solution.find_diff_xor('abcd', 'abcde'), 'e')\n",
130+
" assert_equal(solution.find_diff_xor('aaabbcdd', 'abdbacade'), 'e')\n",
123131
" print('Success: test_find_diff')\n",
124132
"\n",
125133
"\n",

arrays_strings/str_diff/str_diff_solution.ipynb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
" * The strings are lower case\n",
4040
"* Can we assume the inputs are valid?\n",
4141
" * No, check for None\n",
42+
" * Otherwise, assume there is only a single different char between the two strings\n",
4243
"* Can we assume this fits memory?\n",
4344
" * Yes"
4445
]
@@ -50,6 +51,8 @@
5051
"## Test Cases\n",
5152
"\n",
5253
"* None input -> TypeError\n",
54+
"* 'ab', 'aab' -> 'a'\n",
55+
"* 'aab', 'ab' -> 'a'\n",
5356
"* 'abcd', 'abcde' -> 'e'\n",
5457
"* 'aaabbcdd', 'abdbacade' -> 'e'"
5558
]
@@ -65,6 +68,7 @@
6568
"* Keep a dictionary of seen values in s\n",
6669
"* Loop through t, decrementing the seen values\n",
6770
" * If the char is not there or if the decrement results in a negative value, return the char\n",
71+
"* Return the differing char from the dictionary\n",
6872
"\n",
6973
"Complexity:\n",
7074
"* Time: O(m+n), where m and n are the lengths of s, t\n",
@@ -112,7 +116,8 @@
112116
" return char\n",
113117
" if seen[char] < 0:\n",
114118
" return char\n",
115-
" return None\n",
119+
" for char, count in seen.items():\n",
120+
" return char\n",
116121
"\n",
117122
" def find_diff_xor(self, str1, str2):\n",
118123
" if str1 is None or str2 is None:\n",
@@ -157,8 +162,14 @@
157162
" def test_find_diff(self):\n",
158163
" solution = Solution()\n",
159164
" assert_raises(TypeError, solution.find_diff, None)\n",
165+
" assert_equal(solution.find_diff('ab', 'aab'), 'a')\n",
166+
" assert_equal(solution.find_diff('aab', 'ab'), 'a')\n",
160167
" assert_equal(solution.find_diff('abcd', 'abcde'), 'e')\n",
161168
" assert_equal(solution.find_diff('aaabbcdd', 'abdbacade'), 'e')\n",
169+
" assert_equal(solution.find_diff_xor('ab', 'aab'), 'a')\n",
170+
" assert_equal(solution.find_diff_xor('aab', 'ab'), 'a')\n",
171+
" assert_equal(solution.find_diff_xor('abcd', 'abcde'), 'e')\n",
172+
" assert_equal(solution.find_diff_xor('aaabbcdd', 'abdbacade'), 'e')\n",
162173
" print('Success: test_find_diff')\n",
163174
"\n",
164175
"\n",

arrays_strings/str_diff/test_str_diff.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ class TestFindDiff(object):
66
def test_find_diff(self):
77
solution = Solution()
88
assert_raises(TypeError, solution.find_diff, None)
9+
assert_equal(solution.find_diff('ab', 'aab'), 'a')
10+
assert_equal(solution.find_diff('aab', 'ab'), 'a')
911
assert_equal(solution.find_diff('abcd', 'abcde'), 'e')
1012
assert_equal(solution.find_diff('aaabbcdd', 'abdbacade'), 'e')
13+
assert_equal(solution.find_diff_xor('ab', 'aab'), 'a')
14+
assert_equal(solution.find_diff_xor('aab', 'ab'), 'a')
15+
assert_equal(solution.find_diff_xor('abcd', 'abcde'), 'e')
16+
assert_equal(solution.find_diff_xor('aaabbcdd', 'abdbacade'), 'e')
1117
print('Success: test_find_diff')
1218

1319

0 commit comments

Comments
 (0)