mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-12-19 02:07:27 +08:00
perf(strings): optimize anagram signature using frequency counts (#12927)
* fix(strings): use frequency-based signature for anagrams Replaced the sorting-based signature implementation with a frequency-based approach using `collections.Counter`. This ensures that the signature represents both characters and their counts, preventing collisions and better grouping of true anagrams. Examples: - "test" → "e1s1t2" - "finaltest" → "a1e1f1i1l1n1s1t2" - "this is a test" → " 3a1e1h1i2s3t3" Also updated the anagram lookup to use the new frequency-based signatures, making results more accurate and avoiding false positives. * Refactor anagram function return type to list[str] * Update anagrams.py * Update anagrams.py * Update anagrams.py * Update anagrams.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -6,19 +6,26 @@ from pathlib import Path
|
||||
|
||||
|
||||
def signature(word: str) -> str:
|
||||
"""Return a word sorted
|
||||
>>> signature("test")
|
||||
'estt'
|
||||
>>> signature("this is a test")
|
||||
' aehiisssttt'
|
||||
>>> signature("finaltest")
|
||||
'aefilnstt'
|
||||
"""
|
||||
return "".join(sorted(word))
|
||||
Return a word's frequency-based signature.
|
||||
|
||||
>>> signature("test")
|
||||
'e1s1t2'
|
||||
>>> signature("this is a test")
|
||||
' 3a1e1h1i2s3t3'
|
||||
>>> signature("finaltest")
|
||||
'a1e1f1i1l1n1s1t2'
|
||||
"""
|
||||
frequencies = collections.Counter(word)
|
||||
return "".join(
|
||||
f"{char}{frequency}" for char, frequency in sorted(frequencies.items())
|
||||
)
|
||||
|
||||
|
||||
def anagram(my_word: str) -> list[str]:
|
||||
"""Return every anagram of the given word
|
||||
"""
|
||||
Return every anagram of the given word from the dictionary.
|
||||
|
||||
>>> anagram('test')
|
||||
['sett', 'stet', 'test']
|
||||
>>> anagram('this is a test')
|
||||
@@ -40,5 +47,5 @@ if __name__ == "__main__":
|
||||
all_anagrams = {word: anagram(word) for word in word_list if len(anagram(word)) > 1}
|
||||
|
||||
with open("anagrams.txt", "w") as file:
|
||||
file.write("all_anagrams = \n ")
|
||||
file.write("all_anagrams = \n")
|
||||
file.write(pprint.pformat(all_anagrams))
|
||||
|
||||
Reference in New Issue
Block a user