Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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)