-
Notifications
You must be signed in to change notification settings - Fork 1
03: Longest Substring Without Repeating Characters #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| seen_map = {} | ||
| max_len = 0 | ||
| win_start = 0 | ||
|
|
||
| for idx, value in enumerate(s): | ||
| last_index = seen_map.get(value) | ||
| if last_index is not None and last_index >= win_start: | ||
| win_start = last_index + 1 | ||
|
|
||
| seen_map[value] = idx | ||
|
|
||
| max_len = max(max_len, idx - win_start + 1) | ||
|
|
||
| return max_len |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # test file for LeetCode medium problems | ||
|
|
||
| import pytest | ||
|
|
||
| # 03: Longest Substring Without Repeating Characters | ||
| # https://leetcode.com/problems/longest-substring-without-repeating-characters/?envType=problem-list-v2&envId=hash-table | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "s, expected", | ||
| [ | ||
| ("abcabcbb", 3), # longest substring is "abc" with length 3 | ||
| ("bbbbb", 1), # longest substring is "b" with length 1 | ||
| ("pwwkew", 3), # longest substring is "wke" with length 3 | ||
| ("", 0), # empty string, longest substring is 0 | ||
| ("abcdef", 6), # entire string is the longest substring, length 6 | ||
| ("aab", 2), # longest substring is "ab", length 2 | ||
| ("dvdf", 3), # longest substring is "vdf", length 3 | ||
| (" ", 1), # single space character, length 1 | ||
| ], | ||
| ) | ||
| def test_length_of_longest_substring(s: str, expected: int): | ||
| from ..LeetCode.medium.longest_substring_without_repeat import Solution | ||
|
|
||
| solution = Solution() | ||
| assert solution.lengthOfLongestSubstring(s) == expected | ||
|
|
||
|
|
||
| # 05: Longest Palindromic Substring | ||
| # https://leetcode.com/problems/longest-palindromic-substring/?envType=problem-list-v2&envId=hash-table | ||
|
|
||
| # 07: Reverse Integer | ||
| # https://leetcode.com/problems/reverse-integer/?envType=problem-list-v2&envId=hash-table | ||
|
|
||
| # 08: String to Integer (atoi) | ||
| # https://leetcode.com/problems/string-to-integer-atoi/?envType=problem-list-v2&envId=hash-table | ||
|
|
||
| # 09: Palindrome Number | ||
| # https://leetcode.com/problems/palindrome-number/?envType=problem-list-v2&envId=hash-table | ||
|
|
||
| # 10: Regular Expression Matching | ||
| # https://leetcode.com/problems/regular-expression-matching/?envType=problem-list-v2&envId=hash-table | ||
|
|
||
| # 11: Container With Most Water | ||
| # https://leetcode.com/problems/container-with-most-water/?envType=problem-list-v2&envId=hash-table | ||
|
|
||
| # 12: Integer to Roman | ||
| # https://leetcode.com/problems/integer-to-roman/?envType=problem-list-v2&envId=hash-table |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,40 @@ | ||||||||||||||||||||||||||||||||||||||||
| from typing import List | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||||||
| def hasDuplicate(self, nums: List[int]) -> bool: | ||||||||||||||||||||||||||||||||||||||||
| seen = {} # {key: valu} | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| for val in nums: | ||||||||||||||||||||||||||||||||||||||||
| gg = seen.get(val) | ||||||||||||||||||||||||||||||||||||||||
| if gg: | ||||||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||||||
| seen[val] = 1 | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def isAnagram(self, s: str, t: str) -> bool: | ||||||||||||||||||||||||||||||||||||||||
| if len(s) != len(t): | ||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| s_map = {} | ||||||||||||||||||||||||||||||||||||||||
| t_map = {} | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| for idx in range(len(s)): | ||||||||||||||||||||||||||||||||||||||||
| s_char = s[idx] | ||||||||||||||||||||||||||||||||||||||||
| t_chat = t[idx] | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| s_map[s_char] = s_map.get(s_char, 0) + 1 | ||||||||||||||||||||||||||||||||||||||||
| t_map[t_chat] = t_map.get(t_chat, 0) + 1 | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return s_map == t_map | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||||||||||||||||||||||||||||||||||||||||
| hash_map = {} | ||||||||||||||||||||||||||||||||||||||||
| for idx in range(len(nums)): | ||||||||||||||||||||||||||||||||||||||||
| hash_map[nums[idx]] = idx | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| for idx in range(len(nums)): | ||||||||||||||||||||||||||||||||||||||||
| diff = target - nums[idx] | ||||||||||||||||||||||||||||||||||||||||
| if diff in hash_map and hash_map[diff] != idx: | ||||||||||||||||||||||||||||||||||||||||
| return [idx, hash_map[diff]] | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+32
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add explicit return for the no-pair case. The function can implicitly return def twoSum(self, nums: List[int], target: int) -> List[int]:
hash_map = {}
for idx in range(len(nums)):
hash_map[nums[idx]] = idx
for idx in range(len(nums)):
diff = target - nums[idx]
if diff in hash_map and hash_map[diff] != idx:
return [idx, hash_map[diff]]
+ return [] # or raise an exception if input guarantees a solution exists📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typo in variable name.
t_chatshould bet_charfor consistency and clarity.🤖 Prompt for AI Agents