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
36 changes: 36 additions & 0 deletions Go/longest-palindromic-substring.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions Go/majority-element.go
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions Go/roman-to-integer.go
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions Go/valid-parentheses.go
Original file line number Diff line number Diff line change
@@ -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
}
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!")