A matching tuple
1078. Occurrences After Bigram
Requirements: matching the first and second outputs of all the third word after word
Realization of ideas:
Determining word boundaries and word pairs with 1,2, normal processing can be thinking, no special data structure and trick
python3 code is as follows:
class Solution:
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
if text is None:
return []
words = text.split(" ")
print(words)
if len(words)<=2:
return []
print('here')
seqs=[]
seqs = [words[i+2] for i in range(len(words)-2) if words[i] == first and words[i+1] == second]
return seqs
Note: The characters are equal judgment, the choice ==, be careful not to use is.
2 Number of character combinations
1079. Letter Tile Possibilities
Returns the number of combinations of characters, for example, given:
"AAB", the following possible combinations, returns 8"A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA"
Note: are uppercase letters
This question and some of the questions Leetcode, belong to a class of topics, dfs depth-first search and backtracking combined, see:
The ultimate template depth-first search and backtracking combination
from collections import Counter
class Solution:
def numTilePossibilities(self, tiles: str) -> int:
if len(tiles) == 0:
return 0
dt = dict(Counter(tiles))
return self.dfs(dt);
def dfs(self,dt):
sum = 0
for k,v in dt.items():
if v == 0:
continue
sum += 1
dt[k] -= 1
sum += self.dfs(dt)
dt[k] += 1
return sum
Recommended reading:
How scored 10 algorithm engineers offer, not to be missed!
Python Data Analysis Learning Path personal summary
Python and algorithms Community
A good-looking point