From 891689d0a90567b97c3790f5d08476d7b00f02e2 Mon Sep 17 00:00:00 2001 From: t0xa Date: Wed, 21 Jan 2026 21:44:22 +0300 Subject: [PATCH] algocode-hashmap-practice-438 solution --- algocode/hash_tables/anagram.py | 68 +++++++++++++++++++ .../find_all_anagrams_in_a_string.py | 51 ++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 algocode/hash_tables/anagram.py create mode 100644 algocode/hash_tables/find_all_anagrams_in_a_string.py diff --git a/algocode/hash_tables/anagram.py b/algocode/hash_tables/anagram.py new file mode 100644 index 0000000..dd04987 --- /dev/null +++ b/algocode/hash_tables/anagram.py @@ -0,0 +1,68 @@ +""" +Given two strings s and t, return true if t is an anagram of s, and false otherwise. + +Example 1: +Input: s = "anagram", t = "nagaram" +Output: true + +Example 2: +Input: s = "rat", t = "car" +Output: false + +Constraints: +1 <= s.length, t.length <= 5 * 104 +s and t consist of lowercase English letters. + +Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case? +""" + +""" +Анаграмма - слово составленное из одинакового кол-ва букв +Поулчается что по сути - тут надо делать счетчик букв в каждом слове через хэшмапу +""" +from collections import defaultdict + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + word_1 = defaultdict(int) + word_2 = defaultdict(int) + for char in s: + word_1[char] += 1 + for char in t: + word_2[char] += 1 + for char, count in word_2.items(): + """ + { + "n": 1, + "a": 3, + "g": 1, + "r": 1 + } + """ + if not word_1.get(char) == count: + return False + return True + + +sc = Solution() + +s = "anagram" +t = "nagaram" +result = True +solution = sc.isAnagram(s, t) +assert solution == result, f"Solution: {solution}, result: {result}" + +s = "rat" +t = "car" +result = False +solution = sc.isAnagram(s, t) +assert solution == result, f"Solution: {solution}, result: {result}" + +s = "ab" +t = "b" +result = False +solution = sc.isAnagram(s, t) +assert solution == result, f"Solution: {solution}, result: {result}" + diff --git a/algocode/hash_tables/find_all_anagrams_in_a_string.py b/algocode/hash_tables/find_all_anagrams_in_a_string.py new file mode 100644 index 0000000..54c5844 --- /dev/null +++ b/algocode/hash_tables/find_all_anagrams_in_a_string.py @@ -0,0 +1,51 @@ +""" +Working solution from 7 try +Inerview passed: False +""" +from typing import List, Optional +from collections import defaultdict + +class Solution: + def findAnagrams(self, s: str, p: str): + if len(p) > len(s): + return [] + p_len = len(p) + p_count = defaultdict(int) + for char in p: + p_count[char] += 1 + s_len = len(s) + res = [] + cur_pos = 0 + cur_count = defaultdict(int) + # Заполняем предварительный словарь + for i in range(0, p_len): + cur_count[s[i]] += 1 + cur_pos += 1 + if cur_count == p_count: + res.append(0) + cur_pos = 1 + while cur_pos <= s_len - p_len: + # Delete char from prev iteration + d_char = s[cur_pos-1] + cur_count[d_char] -= 1 + if cur_count.get(d_char) == 0: + del cur_count[d_char] + # Adding current iteration last char + a_char = s[cur_pos + p_len - 1] + cur_count[a_char] += 1 + # Compare + print(f"{cur_count}\n{p_count}\n----") + if cur_count == p_count: + res.append(cur_pos) + # Next + cur_pos += 1 + return res + +c = Solution() +s = "cbaebabacd" +p = "abc" +print(c.findAnagrams(s, p)) + +s = "af" +p = "be" +print(c.findAnagrams(s, p))