Skip to content

Commit 1036547

Browse files
authored
Create count-words-obtained-after-adding-a-letter.py
1 parent 2e3af63 commit 1036547

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def wordCount(self, startWords, targetWords):
6+
"""
7+
:type startWords: List[str]
8+
:type targetWords: List[str]
9+
:rtype: int
10+
"""
11+
def bitmask(w):
12+
return reduce(lambda x, y: x|y, (1 << (ord(c)-ord('a')) for i, c in enumerate(w)))
13+
14+
lookup = set(bitmask(w) for w in startWords)
15+
result = 0
16+
for w in targetWords:
17+
mask = bitmask(w)
18+
result += any(mask ^ (1 << ord(c)-ord('a')) in lookup for c in w)
19+
return result

0 commit comments

Comments
 (0)