Skip to content

Commit 7ce4165

Browse files
committed
added abc_create_word
1 parent be136f7 commit 7ce4165

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

python/datastructures/dijkstra.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections import namedtuple, defaultdict
1+
from collections import defaultdict
22

33

44
class Graph:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def recursive_is_valid(s, t=None, memo=None):
2+
if memo is None:
3+
memo = {}
4+
if t is None:
5+
t = ""
6+
7+
if t in memo:
8+
return memo[t]
9+
if s == t:
10+
return True
11+
if len(t) > len(s):
12+
return False
13+
14+
for i in range(len(t)+1):
15+
new_t = t[:i+1] + "abc" + t[i+1:]
16+
17+
if recursive_is_valid(s, new_t, memo):
18+
memo[t] = True
19+
return True
20+
21+
memo[t] = False
22+
return False
23+
24+
def fast_is_valid(s):
25+
while "abc" in s:
26+
s = s.replace("abc", "")
27+
28+
return not s
29+
30+
31+
32+
33+
34+
if __name__ == "__main__":
35+
print(fast_is_valid("aabcbc")) # True
36+
print(fast_is_valid("abcabcababcc")) # True
37+
print(fast_is_valid("abccba")) # False
38+
print(fast_is_valid("abcabcabcabcabcabcabcabcabcabc")) # True

0 commit comments

Comments
 (0)