Skip to content
Open
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
64 changes: 64 additions & 0 deletions Python/217_ContainsDuplicate.py
Original file line number Diff line number Diff line change
@@ -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!")
62 changes: 62 additions & 0 deletions Python/3_LongestSubstringWithoutRepeatingCharacters.py
Original file line number Diff line number Diff line change
@@ -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!")