diff --git a/Go/leetcode.go b/Go/leetcode.go index b3f5528..9075046 100644 --- a/Go/leetcode.go +++ b/Go/leetcode.go @@ -234,10 +234,7 @@ func IsValidParentheses(s string) bool { } } - if len(stack) > 0 { - return false - } - return true + return len(stack) == 0 } // 217. Contains Duplicate @@ -254,3 +251,23 @@ func ContainsDuplicate(nums []int) bool { } return false } + +// 3. Longest Substring Without Repeating Characters +// https://leetcode.com/problems/longest-substring-without-repeating-characters/?envType=problem-list-v2&envId=hash-table +func LengthOfLongestSubstring(s string) int { + charIndex := make(map[rune]int) + maxLen := 0 + start := 0 + for idx, ch := range []rune(s) { + if lastIndex, found := charIndex[ch]; found && lastIndex >= start { + start = lastIndex + 1 + } + charIndex[ch] = idx + + if idx-start+1 > maxLen { + maxLen = idx - start +1 + } + } + + return maxLen +} diff --git a/Go/leetcode_test.go b/Go/leetcode_test.go index f5e4eda..ba62e5c 100644 --- a/Go/leetcode_test.go +++ b/Go/leetcode_test.go @@ -193,3 +193,30 @@ func Test_ContainsDuplicate(t *testing.T) { } } } + +// 3. Longest Substring Without Repeating Characters +// https://leetcode.com/problems/longest-substring-without-repeating-characters/?envType=problem-list-v2&envId=hash-table +func Test_LengthOfLongestSubstring(t *testing.T) { + tests := []struct { + s string + expected int + }{ + {"abcabcbb", 3}, + {"bbbbb", 1}, + {"pwwkew", 3}, + {"", 0}, + {"a", 1}, + {"ab", 2}, + {"dvdf", 3}, + {"tmmzuxt", 5}, + {" ", 1}, + } + + for _, test := range tests { + got := LengthOfLongestSubstring(test.s) + if got != test.expected { + t.Errorf("for %s got %d but want %d", test.s, got, test.expected) + } + } + +} diff --git a/LeetCode/medium/longest_substring_without_repeat.py b/LeetCode/medium/longest_substring_without_repeat.py new file mode 100644 index 0000000..78040c1 --- /dev/null +++ b/LeetCode/medium/longest_substring_without_repeat.py @@ -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 diff --git a/tests/test_leetcode_medium.py b/tests/test_leetcode_medium.py new file mode 100644 index 0000000..d2cdf2f --- /dev/null +++ b/tests/test_leetcode_medium.py @@ -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 diff --git a/weekly_roundtable/array_hasing/nov_09_2025.py b/weekly_roundtable/array_hasing/nov_09_2025.py new file mode 100644 index 0000000..1632272 --- /dev/null +++ b/weekly_roundtable/array_hasing/nov_09_2025.py @@ -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]]