1078. Occurrences After Bigram
要求:输出所有匹配第1、2个单词后的第3个单词
实现思路:
边界判断和1,2单词词对搭配,按正常思路处理即可,无trick和特别的数据结构
python3 代码如下:
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
注意:判断字符相等,选用 == , 注意不要使用 is .
2 字符组合数
1079. Letter Tile Possibilities
返回字符组合数,例如给定:
"AAB",可能的组合如下,返回 8
"A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA"
注意:都为大写字母
这道题与Leetcode一些题,同属于一类题目,深度优先搜索 dfs 和回溯相结合,详见:
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
本文分享自 程序员郭震zhenguo 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!