Skip to content

Commit b02e3bf

Browse files
Hai Hoang Danggoswami-rahul
Hai Hoang Dang
authored andcommitted
PR to add some hash problems (keon#387)
* Add word_pattern in hash map * Add ismorphic to hash table * Add is_anagram to hash table * Add find_keyboard_row to set * Fix the issue
1 parent 508b991 commit b02e3bf

File tree

9 files changed

+179
-1
lines changed

9 files changed

+179
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ If you want to uninstall algorithms, it is as simple as:
179179
- [longest_common_subsequence](algorithms/map/longest_common_subsequence.py)
180180
- [randomized_set](algorithms/map/randomized_set.py)
181181
- [valid_sudoku](algorithms/map/valid_sudoku.py)
182+
- [word_pattern](algorithms/map/word_pattern.py)
183+
- [is_isomorphic](algorithms/map/is_isomorphic.py)
184+
- [is_anagram](algorithms/map/is_anagram.py)
182185
- [maths](algorithms/maths)
183186
- [base_conversion](algorithms/maths/base_conversion.py)
184187
- [combination](algorithms/maths/combination.py)
@@ -231,6 +234,7 @@ If you want to uninstall algorithms, it is as simple as:
231234
- [set](algorithms/set)
232235
- [randomized_set](algorithms/set/randomized_set.py)
233236
- [set_covering](algorithms/set/set_covering.py)
237+
- [find_keyboard_row](algorithms/set/find_keyboard_row.py)
234238
- [sort](algorithms/sort)
235239
- [bitonic_sort](algorithms/sort/bitonic_sort.py)
236240
- [bogo_sort](algorithms/sort/bogo_sort.py)

algorithms/map/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
from .hashtable import *
22
from .separate_chaining_hashtable import *
3+
from .word_pattern import *
4+
from .is_isomorphic import *
5+
from .is_anagram import *

algorithms/map/is_anagram.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Given two strings s and t , write a function to determine if t is an anagram of s.
3+
4+
Example 1:
5+
Input: s = "anagram", t = "nagaram"
6+
Output: true
7+
8+
Example 2:
9+
Input: s = "rat", t = "car"
10+
Output: false
11+
12+
Note:
13+
You may assume the string contains only lowercase alphabets.
14+
15+
Reference: https://leetcode.com/problems/valid-anagram/description/
16+
"""
17+
def is_anagram(s, t):
18+
"""
19+
:type s: str
20+
:type t: str
21+
:rtype: bool
22+
"""
23+
maps = {}
24+
mapt = {}
25+
for i in s:
26+
maps[i] = maps.get(i, 0) + 1
27+
for i in t:
28+
mapt[i] = mapt.get(i, 0) + 1
29+
return maps == mapt

algorithms/map/is_isomorphic.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Given two strings s and t, determine if they are isomorphic.
3+
Two strings are isomorphic if the characters in s can be replaced to get t.
4+
All occurrences of a character must be replaced with another character while
5+
preserving the order of characters. No two characters may map to the same
6+
character but a character may map to itself.
7+
8+
Example 1:
9+
Input: s = "egg", t = "add"
10+
Output: true
11+
12+
Example 2:
13+
Input: s = "foo", t = "bar"
14+
Output: false
15+
16+
Example 3:
17+
Input: s = "paper", t = "title"
18+
Output: true
19+
Reference: https://leetcode.com/problems/isomorphic-strings/description/
20+
"""
21+
def is_isomorphic(s, t):
22+
"""
23+
:type s: str
24+
:type t: str
25+
:rtype: bool
26+
"""
27+
if len(s) != len(t):
28+
return False
29+
dict = {}
30+
set_value = set()
31+
for i in range(len(s)):
32+
if s[i] not in dict:
33+
if t[i] in set_value:
34+
return False
35+
dict[s[i]] = t[i]
36+
set_value.add(t[i])
37+
else:
38+
if dict[s[i]] != t[i]:
39+
return False
40+
return True

algorithms/map/word_pattern.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Given a pattern and a string str, find if str follows the same pattern.
3+
Here follow means a full match, such that there is a bijection between a
4+
letter in pattern and a non-empty word in str.
5+
6+
Example 1:
7+
Input: pattern = "abba", str = "dog cat cat dog"
8+
Output: true
9+
10+
Example 2:
11+
Input:pattern = "abba", str = "dog cat cat fish"
12+
Output: false
13+
14+
Example 3:
15+
Input: pattern = "aaaa", str = "dog cat cat dog"
16+
Output: false
17+
18+
Example 4:
19+
Input: pattern = "abba", str = "dog dog dog dog"
20+
Output: false
21+
Notes:
22+
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
23+
Reference: https://leetcode.com/problems/word-pattern/description/
24+
"""
25+
def word_pattern(pattern, str):
26+
dict = {}
27+
set_value = set()
28+
list_str = str.split()
29+
if len(list_str) != len(pattern):
30+
return False
31+
for i in range(len(pattern)):
32+
if pattern[i] not in dict:
33+
if list_str[i] in set_value:
34+
return False
35+
dict[pattern[i]] = list_str[i]
36+
set_value.add(list_str[i])
37+
else:
38+
if dict[pattern[i]] != list_str[i]:
39+
return False
40+
return True

algorithms/set/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .find_keyboard_row import *

algorithms/set/find_keyboard_row.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Given a List of words, return the words that can be typed using letters of
3+
alphabet on only one row's of American keyboard.
4+
5+
For example:
6+
Input: ["Hello", "Alaska", "Dad", "Peace"]
7+
Output: ["Alaska", "Dad"]
8+
9+
Reference: https://leetcode.com/problems/keyboard-row/description/
10+
"""
11+
def find_keyboard_row(words):
12+
"""
13+
:type words: List[str]
14+
:rtype: List[str]
15+
"""
16+
keyboard = [
17+
set('qwertyuiop'),
18+
set('asdfghjkl'),
19+
set('zxcvbnm'),
20+
]
21+
result = []
22+
for word in words:
23+
for key in keyboard:
24+
if set(word.lower()).issubset(key):
25+
result.append(word)
26+
return result

tests/test_map.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from algorithms.map import (
22
HashTable, ResizableHashTable,
3-
Node, SeparateChainingHashTable
3+
Node, SeparateChainingHashTable,
4+
word_pattern,
5+
is_isomorphic,
6+
is_anagram
47
)
58

69
import unittest
@@ -147,6 +150,28 @@ def test_get_none_if_key_missing_and_hash_collision(self):
147150
self.assertEqual(None, m.get(11))
148151

149152

153+
class TestWordPattern(unittest.TestCase):
154+
def test_word_pattern(self):
155+
self.assertTrue(word_pattern("abba", "dog cat cat dog"))
156+
self.assertFalse(word_pattern("abba", "dog cat cat fish"))
157+
self.assertFalse(word_pattern("abba", "dog dog dog dog"))
158+
self.assertFalse(word_pattern("aaaa", "dog cat cat dog"))
159+
160+
161+
class TestIsSomorphic(unittest.TestCase):
162+
def test_is_isomorphic(self):
163+
self.assertTrue(is_isomorphic("egg", "add"))
164+
self.assertFalse(is_isomorphic("foo", "bar"))
165+
self.assertTrue(is_isomorphic("paper", "title"))
166+
167+
168+
class TestIsAnagram(unittest.TestCase):
169+
def test_is_anagram(self):
170+
self.assertTrue(is_anagram("anagram", "nagaram"))
171+
self.assertFalse(is_anagram("rat", "car"))
172+
173+
174+
150175

151176
if __name__ == "__main__":
152177
unittest.main()

tests/test_set.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from algorithms.set import (
2+
find_keyboard_row
3+
)
4+
5+
import unittest
6+
7+
class TestFindKeyboardRow(unittest.TestCase):
8+
def test_find_keyboard_row(self):
9+
self.assertEqual(["Alaska", "Dad"],
10+
find_keyboard_row(["Hello", "Alaska", "Dad", "Peace"]))

0 commit comments

Comments
 (0)