From 5bedc615a731ae5636b3b830cbf489a8d8e49979 Mon Sep 17 00:00:00 2001 From: sarthaksharma1177-rgb Date: Sun, 22 Feb 2026 03:02:04 +0530 Subject: [PATCH 1/6] Add containsDuplicate function and test cases Implement containsDuplicate function to check for duplicates in an integer array using a set. Also, add an alternative approach using set length comparison. --- Python/217_ContainsDuplicate.py | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Python/217_ContainsDuplicate.py diff --git a/Python/217_ContainsDuplicate.py b/Python/217_ContainsDuplicate.py new file mode 100644 index 00000000..79a08e60 --- /dev/null +++ b/Python/217_ContainsDuplicate.py @@ -0,0 +1,64 @@ +""" +217. Contains Duplicate +Easy + +Given an integer array nums, return true if any value appears at least twice +in the array, and return false if every element is distinct. + +Constraints: +- 1 <= nums.length <= 10^5 +- -10^9 <= nums[i] <= 10^9 +""" + +def containsDuplicate(nums): + """ + Approach: Using HashSet + Time Complexity: O(n) + Space Complexity: O(n) + + Uses a set to track seen numbers. If we encounter a number already in the set, + we have a duplicate. + """ + seen = set() + for num in nums: + if num in seen: + return True + seen.add(num) + return False + + +# Alternative approach using len comparison (more concise) +def containsDuplicate_v2(nums): + """ + Approach: Set length comparison + Time Complexity: O(n) + Space Complexity: O(n) + + If there are duplicates, the set will have fewer elements than the list. + """ + return len(nums) != len(set(nums)) + + +# Test cases +if __name__ == "__main__": + # Test case 1: Array with duplicate + assert containsDuplicate([1, 2, 3, 1]) == True + assert containsDuplicate_v2([1, 2, 3, 1]) == True + + # Test case 2: Array without duplicate + assert containsDuplicate([1, 2, 3, 4]) == False + assert containsDuplicate_v2([1, 2, 3, 4]) == False + + # Test case 3: Single element + assert containsDuplicate([1]) == False + assert containsDuplicate_v2([1]) == False + + # Test case 4: Two elements with duplicate + assert containsDuplicate([1, 1]) == True + assert containsDuplicate_v2([1, 1]) == True + + # Test case 5: Duplicate at end + assert containsDuplicate([99, 99]) == True + assert containsDuplicate_v2([99, 99]) == True + + print("All test cases passed!") From add51ca22b5cd60bb3190b2717dea28990ae6229 Mon Sep 17 00:00:00 2001 From: sarthaksharma1177-rgb Date: Sun, 22 Feb 2026 03:04:55 +0530 Subject: [PATCH 2/6] Add solution for longest substring without repeating characters Implement the solution for finding the length of the longest substring without repeating characters using a sliding window approach. --- ...gestSubstringWithoutRepeatingCharacters.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Python/3_LongestSubstringWithoutRepeatingCharacters.py diff --git a/Python/3_LongestSubstringWithoutRepeatingCharacters.py b/Python/3_LongestSubstringWithoutRepeatingCharacters.py new file mode 100644 index 00000000..e2465f0c --- /dev/null +++ b/Python/3_LongestSubstringWithoutRepeatingCharacters.py @@ -0,0 +1,62 @@ +""" +3. Longest Substring Without Repeating Characters +Medium + +Given a string s, find the length of the longest substring +without repeating characters. + +Constraints: +- 0 <= s.length <= 5 * 10^4 +- s consists of English letters, digits, symbols and spaces. +""" + +def lengthOfLongestSubstring(s): + """ + Approach: Sliding Window with HashMap + Time Complexity: O(n) + Space Complexity: O(min(m, n)) where m is charset size + + Use a sliding window approach with a hash map to track character positions. + When we encounter a repeating character, move the window start. + """ + char_index = {} + max_length = 0 + start = 0 + + for end in range(len(s)): + if s[end] in char_index and char_index[s[end]] >= start: + start = char_index[s[end]] + 1 + + char_index[s[end]] = end + max_length = max(max_length, end - start + 1) + + return max_length + + +# Test cases +if __name__ == "__main__": + # Test case 1: String with repeating characters + assert lengthOfLongestSubstring("abcabcbb") == 3 # "abc" + + # Test case 2: String with all unique characters + assert lengthOfLongestSubstring("bbbbb") == 1 # "b" + + # Test case 3: String with mix of characters + assert lengthOfLongestSubstring("pwwkew") == 3 # "wke" + + # Test case 4: Empty string + assert lengthOfLongestSubstring("") == 0 + + # Test case 5: Single character + assert lengthOfLongestSubstring("a") == 1 + + # Test case 6: All unique characters + assert lengthOfLongestSubstring("abcdefg") == 7 + + # Test case 7: Space character + assert lengthOfLongestSubstring("au") == 2 + + # Test case 8: Digits and symbols + assert lengthOfLongestSubstring("0123456789") == 10 + + print("All test cases passed!") From d788c30252b30b74dcaf3041c9af1e26be51ff8a Mon Sep 17 00:00:00 2001 From: sarthaksharma1177-rgb Date: Mon, 23 Mar 2026 21:07:51 +0530 Subject: [PATCH 3/6] Add Go solution for longest palindromic substring (LeetCode #5) Implement solution for finding the longest palindromic substring. --- Go/longest-palindromic-substring.go | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Go/longest-palindromic-substring.go diff --git a/Go/longest-palindromic-substring.go b/Go/longest-palindromic-substring.go new file mode 100644 index 00000000..104e3e59 --- /dev/null +++ b/Go/longest-palindromic-substring.go @@ -0,0 +1,36 @@ +// LeetCode Problem 5: Longest Palindromic Substring - Medium +package main + +func longestPalindrome(s string) string { + if len(s) < 2 { + return s + } + start, maxLen := 0, 1 + for i := 0; i < len(s); i++ { + // Odd length palindromes + len1 := expandAroundCenter(s, i, i) + // Even length palindromes + len2 := expandAroundCenter(s, i, i+1) + len := max(len1, len2) + if len > maxLen { + maxLen = len + start = i - (len-1)/2 + } + } + return s[start : start+maxLen] +} + +func expandAroundCenter(s string, left, right int) int { + for left >= 0 && right < len(s) && s[left] == s[right] { + left-- + right++ + } + return right - left - 1 +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} From 39bf790d0a9083386233cbb21ac5aa25f9039a1e Mon Sep 17 00:00:00 2001 From: sarthaksharma1177-rgb Date: Mon, 23 Mar 2026 21:09:27 +0530 Subject: [PATCH 4/6] Add Go solution for valid parentheses (LeetCode #20) Implement function to check for valid parentheses using a stack. --- Go/valid-parentheses.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Go/valid-parentheses.go diff --git a/Go/valid-parentheses.go b/Go/valid-parentheses.go new file mode 100644 index 00000000..9117717f --- /dev/null +++ b/Go/valid-parentheses.go @@ -0,0 +1,18 @@ +// LeetCode Problem 20: Valid Parentheses - Medium +package main + +func isValid(s string) bool { + stack := []rune{} + pairs := map[rune]rune{')': '(', '}': '{', ']': '['} + for _, char := range s { + if pair, ok := pairs[char]; ok { + if len(stack) == 0 || stack[len(stack)-1] != pair { + return false + } + stack = stack[:len(stack)-1] + } else { + stack = append(stack, char) + } + } + return len(stack) == 0 +} From 70ef69012380c94d83a6bc2456117dd3f3ce8b55 Mon Sep 17 00:00:00 2001 From: sarthaksharma1177-rgb Date: Mon, 23 Mar 2026 21:12:42 +0530 Subject: [PATCH 5/6] Add Go solution for majority element (LeetCode #169) --- Go/majority-element.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Go/majority-element.go diff --git a/Go/majority-element.go b/Go/majority-element.go new file mode 100644 index 00000000..6190862f --- /dev/null +++ b/Go/majority-element.go @@ -0,0 +1,17 @@ +// LeetCode Problem 169: Majority Element - Medium +package main + +func majorityElement(nums []int) int { + count := 0 + candidate := 0 + for _, num := range nums { + if count == 0 { + candidate = num + } + if num == candidate { + count++ + } else { + count-- + } + } + return candidate From 84a51ff8e96c426901065122be71fd0e9e5e473e Mon Sep 17 00:00:00 2001 From: sarthaksharma1177-rgb Date: Mon, 23 Mar 2026 21:14:06 +0530 Subject: [PATCH 6/6] Add Go solution for roman to integer (LeetCode #13) --- Go/roman-to-integer.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Go/roman-to-integer.go diff --git a/Go/roman-to-integer.go b/Go/roman-to-integer.go new file mode 100644 index 00000000..58c2325d --- /dev/null +++ b/Go/roman-to-integer.go @@ -0,0 +1,22 @@ +// LeetCode Problem 13: Roman to Integer - Medium +package main + +func romanToInt(s string) int { + romanMap := map[rune]int{ + 'I': 1, + 'V': 5, + 'X': 10, + 'L': 50, + 'C': 100, + 'D': 500, + 'M': 1000, + } + total := 0 + for i := 0; i < len(s); i++ { + if i+1 < len(s) && romanMap[rune(s[i])] < romanMap[rune(s[i+1])] { + total -= romanMap[rune(s[i])] + } else { + total += romanMap[rune(s[i])] + } + } + return total