Skip to content

Commit ba382a5

Browse files
authored
Create check-if-string-is-a-prefix-of-array.py
1 parent 52d7717 commit ba382a5

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def isPrefixString(self, s, words):
6+
"""
7+
:type s: str
8+
:type words: List[str]
9+
:rtype: bool
10+
"""
11+
i = j = 0
12+
for c in s:
13+
if i == len(words) or words[i][j] != c:
14+
return False
15+
j += 1
16+
if j == len(words[i]):
17+
i += 1
18+
j = 0
19+
return j == 0
20+
21+
22+
# Time: O(n)
23+
# Space: O(1)
24+
class Solution2(object):
25+
def isPrefixString(self, s, words):
26+
"""
27+
:type s: str
28+
:type words: List[str]
29+
:rtype: bool
30+
"""
31+
i = 0
32+
for word in words:
33+
for c in word:
34+
if i == len(s) or s[i] != c:
35+
return False
36+
i += 1
37+
if i == len(s):
38+
return True
39+
return False

0 commit comments

Comments
 (0)