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.
- Loading branch information
Showing
4 changed files
with
99 additions
and
8 deletions.
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
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 substring in str. | ||
Examples: | ||
pattern = "abab", str = "redblueredblue" should return true. | ||
pattern = "aaaa", str = "asdasdasdasd" should return true. | ||
pattern = "aabb", str = "xyzabcxzyabc" should return false. | ||
Notes: | ||
You may assume both pattern and str contains only lowercase letters. | ||
""" | ||
|
||
|
||
def pattern_match(pattern, string): | ||
""" | ||
:type pattern: str | ||
:type string: str | ||
:rtype: bool | ||
""" | ||
return DFS(pattern, string, {}) | ||
|
||
|
||
def DFS(pattern, string, dic): | ||
print(dic) | ||
if len(pattern) == 0 and len(string) > 0: | ||
return False | ||
if len(pattern) == len(string) == 0: | ||
return True | ||
for end in range(1, len(string)-len(pattern)+2): | ||
if pattern[0] not in dic and string[:end] not in dic.values(): | ||
dic[pattern[0]] = string[:end] | ||
if DFS(pattern[1:], string[end:], dic): | ||
return True | ||
del dic[pattern[0]] | ||
elif pattern[0] in dic and dic[pattern[0]] == string[:end]: | ||
if DFS(pattern[1:], string[end:], dic): | ||
return True | ||
return False | ||
|
||
if __name__ == "__main__": | ||
pattern1 = "abab" | ||
string1 = "redblueredblue" | ||
pattern2 = "aaaa" | ||
string2 = "asdasdasdasd" | ||
pattern3 = "aabb" | ||
string3 = "xyzabcxzyabc" | ||
print(pattern_match(pattern1, string1)) | ||
print(pattern_match(pattern2, string2)) | ||
print(pattern_match(pattern3, string3)) |
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,40 @@ | ||
""" | ||
Design a data structure that supports all following operations | ||
in average O(1) time. | ||
insert(val): Inserts an item val to the set if not already present. | ||
remove(val): Removes an item val from the set if present. | ||
get_random: Returns a random element from current set of elements. | ||
Each element must have the same probability of being returned. | ||
""" | ||
|
||
|
||
class RandomizedSet(): | ||
""" | ||
idea: | ||
shit | ||
""" | ||
def __init__(self): | ||
pass | ||
|
||
def insert(self, val): | ||
pass | ||
|
||
def remove(self, val): | ||
pass | ||
|
||
def get_random(self): | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
rset = RandomizedSet() | ||
rset.insert(1) | ||
rset.insert(2) | ||
rset.insert(3) | ||
|
||
rset.remove(2) | ||
|
||
print(rset.get_random()) | ||
print(rset.get_random()) | ||
print(rset.get_random()) |