diff --git a/algocode/hash_tables/3sum.py b/algocode/hash_tables/3sum.py new file mode 100644 index 0000000..ea74cf4 --- /dev/null +++ b/algocode/hash_tables/3sum.py @@ -0,0 +1,36 @@ +from typing import List + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + sorted = nums.sort() + result = [] + for i in range(0, len(nums) - 2): + if i > 0 and nums[i] == nums[i-1]: + i += 1 + continue + j = i + 1 + k = len(nums) - 1 + first_num = nums[i] + while j < k: + second_num = nums[j] + third_num = nums[k] + tmp_sum = first_num + second_num + third_num + if tmp_sum < 0: + j += 1 + elif tmp_sum > 0: + k -= 1 + elif tmp_sum == 0: + j += 1 + result.append([first_num, second_num, third_num]) + while nums[j-1] == nums[j] and j < k: + j += 1 + else: + raise ValueError("Some strange shit happened") + i += 1 + return result + + +cl = Solution() +print(cl.threeSum(nums=[-1,0,1,2,-1,4])) +print(cl.threeSum(nums=[0,0,0])) +print(cl.threeSum(nums=[0,0,0,0])) diff --git a/algocode/hash_tables/isomorphic_strings.py b/algocode/hash_tables/isomorphic_strings.py new file mode 100644 index 0000000..349caca --- /dev/null +++ b/algocode/hash_tables/isomorphic_strings.py @@ -0,0 +1,74 @@ +class Solution: + def isIsomorphic(self, s: str, t: str) -> bool: + res = {} + already_mapped = set() + for i in range(0, len(s)): + s_word = s[i] + t_word = t[i] + if t_word in already_mapped and s_word not in res: + return False + match = res.get(s_word) + # Если не встречался символ + if not match: + # Пока слова изоморфны - мапим новую пару + res[s_word] = t_word + already_mapped.add(t_word) + continue + if match != t_word: + return False + already_mapped.add(t_word) + # Если массив закончился и не было возврата False => слова изоморфны + return True + + +""" +STEP 1 +s = "badc" +t = "baba" + +res = {} +match = None +res = {"b": "b"} + +STEP 2 +s = "badc" +t = "baba" + +res = {"b": "b"} +match = None +res = {"b": "b", "a":"a"} + +STEP 3 +s = "badc" +t = "baba" + +res = {"b": "b", "a":"a"} +match = None +res = {"b": "b", "a":"a", "d": "b"} <- Тут двойной маппинг => надо еще вести уже замапленые символы из t +""" + +cl = Solution() +# s = "egg" +# t = "add" +# print(cl.isIsomorphic(s, t)) +# +# s = "foo" +# t = "bar" +# print(cl.isIsomorphic(s, t)) +# +# s = "paper" +# t = "title" +# print(cl.isIsomorphic(s, t)) +# +# +# s = "p" +# t = "t" +# print(cl.isIsomorphic(s, t)) +# +# s = "p" +# t = "p" +# print(cl.isIsomorphic(s, t)) + +s = "badc" +t = "baba" +print(cl.isIsomorphic(s, t)) diff --git a/algocode/two_pointers/unurlify.py b/algocode/two_pointers/unurlify.py new file mode 100644 index 0000000..d08441a --- /dev/null +++ b/algocode/two_pointers/unurlify.py @@ -0,0 +1,86 @@ +""" +Дан список s. Нужно заменить все вхождения %20 на пробелы в списке s, а оставшиеся лишние символы заменить на #, сохранив длину списка. В качестве ответа верни изменённый список s. + +Пример 1: + +Ввод: s = ["h","e","l","l","o","%","2","0","w","o","r","l","d"] +Вывод: ["h","e","l","l","o"," ","w","o","r","l","d","#","#"] +Пример 2: + +Ввод: s = ["a","%","2","0","b","%","2","0","%","2","0", "c"] +Вывод: ["a"," ","b"," ", " ", "c","#","#","#","#","#","#"] +Ограничения: + +len(s) >= 1 +""" + +from typing import List + + +def solve(s: List[str]) -> List[str]: + i, j = 0, 0 + s_len = len(s) + for i in range(0, s_len): + if j >= s_len: + while i < s_len: + s[i] = "#" + i += 1 + break + s[i] = s[j] + if s[i] == "%": + s[i] = " " + j += 3 + i += 1 + else: + i += 1 + j += 1 + return s + + +print(solve(s=["h", "e", "l", "l", "o", "%", "2", "0", "w", "o", "r", "l", "d"])) +print(solve(s=["a", "%", "2", "0", "b", "%", "2", "0", "%", "2", "0", "c"])) + +# i +# "a","%","2","0","b","%","2","0","%","2","0", "c" +# j +# +# --- i+=1 j+=1 --- +# +# i +# "a","%","2","0","b","%","2","0","%","2","0", "c" +# j +# +# Заменяем процент на пробел +# +# --- i+=1 j+=3 --- +# +# i +# "a"," ","2","0","b","%","2","0","%","2","0", "c" +# j +# +# --- i+=1 j+=1 --- +# +# i +# "a"," ","b","0","b","%","2","0","%","2","0", "c" +# j +# Замена s[i] = s[j] приводит к процентной итерации +# +# --- i+=1 j+=3 --- +# +# i +# "a"," ","b"," ","b","%","2","0","%","2","0", "c" +# j +# +# Замена s[i] = s[j] приводит к процентной итерации +# +# --- i+=1 j+=3 --- +# +# i +# "a"," ","b"," "," ","%","2","0","%","2","0", "c" +# j +# +# --- i+=1 j+=1 --- +# +# i +# "a"," ","b"," "," ","c","2","0","%","2","0", "c" +# j