Skip to content

Commit 4560f3f

Browse files
committed
Update word-pattern.py
1 parent dfe327a commit 4560f3f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Python/word-pattern.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Time: O(n)
2-
# Space: O(c), c is unique count of pattern and words
2+
# Space: O(c), c is unique count of pattern
33

44
# Given a pattern and a string str, find if str follows the same pattern.
55
#
@@ -30,7 +30,7 @@ def wordPattern(self, pattern, str):
3030
w2p, p2w = {}, {}
3131
for p, w in izip(pattern, self.wordGenerator(str)):
3232
if w not in w2p and p not in p2w:
33-
# Build mapping.
33+
# Build mapping. Space: O(c)
3434
w2p[w] = p
3535
p2w[p] = w
3636
elif w not in w2p or w2p[w] != p:
@@ -66,14 +66,14 @@ def wordPattern(self, pattern, str):
6666
:type str: str
6767
:rtype: bool
6868
"""
69-
words = str.split()
69+
words = str.split() # Space: O(n)
7070
if len(pattern) != len(words):
7171
return False
7272

7373
w2p, p2w = {}, {}
7474
for p, w in izip(pattern, words):
7575
if w not in w2p and p not in p2w:
76-
# Build mapping.
76+
# Build mapping. Space: O(c)
7777
w2p[w] = p
7878
p2w[p] = w
7979
elif w not in w2p or w2p[w] != p:

0 commit comments

Comments
 (0)