タイトル説明
二つの文字列sとtを考えると、tは単語の異所性の文字かどうかを判断する関数を書きます。
例1:
入力:S = "アナグラム"、T = "nagaram"
出力:真の
例2:
入力:S =「ラット」、T =「車」
出力:偽
の説明:
あなたは、文字列が小文字のみが含まれているとすることができます。
トピックソリューション
方法1:単語頻度統計を比較します
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
from collections import Counter
maps = Counter(s)
mapt = Counter(t)
return maps == mapt
方法2:統計の手紙号
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
c1 = [0]*26
c2 = [0]*26
for c in s:
pos = ord(c) - ord('a')
c1[pos] = c1[pos] + 1
for c in t:
pos = ord(c) - ord('a')
c2[pos] = c2[pos] + 1
return c1 == c2
トピックを展開します。
出典:https://www.v2ex.com/t/624125
質問は尋ねた:二つの単語の比較、スプライシング文字で単語の後かどうかの単語の最初の一見を。
def can_be_composed(a, b):
a_count = collections.Counter(a)
b_count = collections.Counter(b)
return all(a_count[k] <= b_count.get(k, 0) for k in a_count)
<して思考の同じ列は、単語の頻度は、すべてのすべての条件が満たされていることを確認し使用して、=、単に等号ありません。