diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_31_longest_substring_without_repeating_characters.py b/src/my_project/interviews/top_150_questions_round_22/ex_31_longest_substring_without_repeating_characters.py new file mode 100644 index 00000000..e5519599 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_31_longest_substring_without_repeating_characters.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + char_index = {} + left = 0 + max_length = 0 + + for right in range(len(s)): + # If character seen before and in current window + if s[right] in char_index and char_index[s[right]] >= left: + # Move left pointer past the last occurrence + left = char_index[s[right]] + 1 + + # Update last seen index of current character + char_index[s[right]] = right + + # Update max length + max_length = max(max_length, right - left + 1) + + return max_length \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_32_substring_concat_all_words.py b/src/my_project/interviews/top_150_questions_round_22/ex_32_substring_concat_all_words.py new file mode 100644 index 00000000..2c7afb23 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_32_substring_concat_all_words.py @@ -0,0 +1,9 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import collections + +class Solution: + def findSubstring(self, s: str, words: List[str]) -> List[int]: + if not words: return [] + LS, M, N, C = len(s), len(words), len(words[0]), collections.Counter(words) + return [i for i in range(LS-M*N+1) if collections.Counter([s[a:a+N] for a in range(i,i+M*N,N)]) == C] \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_31_longest_substring_without_repeating_characters_round_22.py b/tests/test_150_questions_round_22/test_31_longest_substring_without_repeating_characters_round_22.py new file mode 100644 index 00000000..196ddbb9 --- /dev/null +++ b/tests/test_150_questions_round_22/test_31_longest_substring_without_repeating_characters_round_22.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_31_longest_substring_without_repeating_characters import Solution + +class LongestSubstringUniqueCharactersTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.lengthOfLongestSubstring(s = "abcabcbb") + target = 3 + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.lengthOfLongestSubstring(s = "bbbbb") + target = 1 + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_32_substring_concat_all_words_round_22.py b/tests/test_150_questions_round_22/test_32_substring_concat_all_words_round_22.py new file mode 100644 index 00000000..dca63851 --- /dev/null +++ b/tests/test_150_questions_round_22/test_32_substring_concat_all_words_round_22.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_32_substring_concat_all_words import Solution + +class SubstringConcatAllWordsTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.findSubstring(s = "barfoothefoobarman", words = ["foo","bar"]) + target = [0,9] + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.findSubstring(s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]) + target = [] + self.assertEqual(output, target) \ No newline at end of file