algocode-hashmap-practice-438 solution
This commit is contained in:
parent
33798cf043
commit
891689d0a9
2 changed files with 119 additions and 0 deletions
68
algocode/hash_tables/anagram.py
Normal file
68
algocode/hash_tables/anagram.py
Normal file
|
|
@ -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}"
|
||||||
|
|
||||||
51
algocode/hash_tables/find_all_anagrams_in_a_string.py
Normal file
51
algocode/hash_tables/find_all_anagrams_in_a_string.py
Normal file
|
|
@ -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))
|
||||||
Loading…
Reference in a new issue