forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
508b991
commit b02e3bf
Showing
9 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
from .hashtable import * | ||
from .separate_chaining_hashtable import * | ||
from .word_pattern import * | ||
from .is_isomorphic import * | ||
from .is_anagram import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
Given two strings s and t , write a function to determine if t is an anagram of s. | ||
Example 1: | ||
Input: s = "anagram", t = "nagaram" | ||
Output: true | ||
Example 2: | ||
Input: s = "rat", t = "car" | ||
Output: false | ||
Note: | ||
You may assume the string contains only lowercase alphabets. | ||
Reference: https://leetcode.com/problems/valid-anagram/description/ | ||
""" | ||
def is_anagram(s, t): | ||
""" | ||
:type s: str | ||
:type t: str | ||
:rtype: bool | ||
""" | ||
maps = {} | ||
mapt = {} | ||
for i in s: | ||
maps[i] = maps.get(i, 0) + 1 | ||
for i in t: | ||
mapt[i] = mapt.get(i, 0) + 1 | ||
return maps == mapt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Given two strings s and t, determine if they are isomorphic. | ||
Two strings are isomorphic if the characters in s can be replaced to get t. | ||
All occurrences of a character must be replaced with another character while | ||
preserving the order of characters. No two characters may map to the same | ||
character but a character may map to itself. | ||
Example 1: | ||
Input: s = "egg", t = "add" | ||
Output: true | ||
Example 2: | ||
Input: s = "foo", t = "bar" | ||
Output: false | ||
Example 3: | ||
Input: s = "paper", t = "title" | ||
Output: true | ||
Reference: https://leetcode.com/problems/isomorphic-strings/description/ | ||
""" | ||
def is_isomorphic(s, t): | ||
""" | ||
:type s: str | ||
:type t: str | ||
:rtype: bool | ||
""" | ||
if len(s) != len(t): | ||
return False | ||
dict = {} | ||
set_value = set() | ||
for i in range(len(s)): | ||
if s[i] not in dict: | ||
if t[i] in set_value: | ||
return False | ||
dict[s[i]] = t[i] | ||
set_value.add(t[i]) | ||
else: | ||
if dict[s[i]] != t[i]: | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Given a pattern and a string str, find if str follows the same pattern. | ||
Here follow means a full match, such that there is a bijection between a | ||
letter in pattern and a non-empty word in str. | ||
Example 1: | ||
Input: pattern = "abba", str = "dog cat cat dog" | ||
Output: true | ||
Example 2: | ||
Input:pattern = "abba", str = "dog cat cat fish" | ||
Output: false | ||
Example 3: | ||
Input: pattern = "aaaa", str = "dog cat cat dog" | ||
Output: false | ||
Example 4: | ||
Input: pattern = "abba", str = "dog dog dog dog" | ||
Output: false | ||
Notes: | ||
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space. | ||
Reference: https://leetcode.com/problems/word-pattern/description/ | ||
""" | ||
def word_pattern(pattern, str): | ||
dict = {} | ||
set_value = set() | ||
list_str = str.split() | ||
if len(list_str) != len(pattern): | ||
return False | ||
for i in range(len(pattern)): | ||
if pattern[i] not in dict: | ||
if list_str[i] in set_value: | ||
return False | ||
dict[pattern[i]] = list_str[i] | ||
set_value.add(list_str[i]) | ||
else: | ||
if dict[pattern[i]] != list_str[i]: | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .find_keyboard_row import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Given a List of words, return the words that can be typed using letters of | ||
alphabet on only one row's of American keyboard. | ||
For example: | ||
Input: ["Hello", "Alaska", "Dad", "Peace"] | ||
Output: ["Alaska", "Dad"] | ||
Reference: https://leetcode.com/problems/keyboard-row/description/ | ||
""" | ||
def find_keyboard_row(words): | ||
""" | ||
:type words: List[str] | ||
:rtype: List[str] | ||
""" | ||
keyboard = [ | ||
set('qwertyuiop'), | ||
set('asdfghjkl'), | ||
set('zxcvbnm'), | ||
] | ||
result = [] | ||
for word in words: | ||
for key in keyboard: | ||
if set(word.lower()).issubset(key): | ||
result.append(word) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from algorithms.set import ( | ||
find_keyboard_row | ||
) | ||
|
||
import unittest | ||
|
||
class TestFindKeyboardRow(unittest.TestCase): | ||
def test_find_keyboard_row(self): | ||
self.assertEqual(["Alaska", "Dad"], | ||
find_keyboard_row(["Hello", "Alaska", "Dad", "Peace"])) |