マッチングタプル
1078出現した後、バイグラム
要件:単語の後、すべての第3ワードの第一および第二の出力をマッチング
アイデアの実現:
1,2と単語の境界と単語のペアを決定し、通常の処理は思考、特別なデータ構造やトリックすることができ
次のようにの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
注:文字が同じ判断され、選択は==、使用しないように注意していること。
文字の組み合わせの2数
1079手紙タイルの可能性
与えられた、例えば、文字の組み合わせの数を返します。
「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
推奨読書:
どのように10人のアルゴリズムエンジニアが見逃すことはない、提供得点!
Pythonとアルゴリズムコミュニティ
見栄えの良いポイント