Skip to content

Commit 2e4932a

Browse files
authored
Merge pull request #1485 from ivan1016017/december14
adding algo
2 parents 3e7481b + 949ed23 commit 2e4932a

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def lengthOfLongestSubstring(self, s: str) -> int:
6+
char_index = {}
7+
left = 0
8+
max_length = 0
9+
10+
for right in range(len(s)):
11+
# If character seen before and in current window
12+
if s[right] in char_index and char_index[s[right]] >= left:
13+
# Move left pointer past the last occurrence
14+
left = char_index[s[right]] + 1
15+
16+
# Update last seen index of current character
17+
char_index[s[right]] = right
18+
19+
# Update max length
20+
max_length = max(max_length, right - left + 1)
21+
22+
return max_length
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import collections
4+
5+
class Solution:
6+
def findSubstring(self, s: str, words: List[str]) -> List[int]:
7+
if not words: return []
8+
LS, M, N, C = len(s), len(words), len(words[0]), collections.Counter(words)
9+
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]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_31_longest_substring_without_repeating_characters import Solution
4+
5+
class LongestSubstringUniqueCharactersTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.lengthOfLongestSubstring(s = "abcabcbb")
10+
target = 3
11+
self.assertEqual(output, target)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.lengthOfLongestSubstring(s = "bbbbb")
16+
target = 1
17+
self.assertEqual(output, target)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_32_substring_concat_all_words import Solution
4+
5+
class SubstringConcatAllWordsTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.findSubstring(s = "barfoothefoobarman", words = ["foo","bar"])
10+
target = [0,9]
11+
self.assertEqual(output, target)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.findSubstring(s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"])
16+
target = []
17+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)