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
67 changes: 67 additions & 0 deletions leetcode205.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 9 20:13:50 2026

@author: rishigoswamy

Problem:
----------
https://leetcode.com/problems/isomorphic-strings/description/

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to
get t, such that:
- Each character maps to exactly one character.
- No two characters map to the same character.
- The order of characters is preserved.

Approach:
----------
We use a hash map to store the mapping from characters in string s
to characters in string t.

As we iterate through both strings simultaneously, for each character
in s, we check:
- If it has not been mapped before, ensure that the corresponding
character in t has not already been used (to maintain a one-to-one
mapping).
- If it has been mapped before, verify that it maps to the same
character in t.

A set is used to track already-mapped characters in t to prevent
multiple characters in s from mapping to the same character.

If all characters satisfy these conditions, the strings are isomorphic.

Time Complexity:
----------------
O(n), where n is the length of the strings.
Each character is processed exactly once.

Space Complexity:
-----------------
O(1), ds and t consist of any valid ascii character.

"""

class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:

sourceMap = {}
mappedCharSet = set()

for i in range(0, len(s)):
if s[i] not in sourceMap:
if t[i] in mappedCharSet:
return False
sourceMap[s[i]] = t[i]
mappedCharSet.add(t[i])

if s[i] in sourceMap:
if sourceMap[s[i]] != t[i]:
return False

return True

66 changes: 66 additions & 0 deletions leetcode290.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 9 20:17:44 2026

@author: rishigoswamy

Problem:
----------
https://leetcode.com/problems/word-pattern/

Given a pattern and a string s, determine if s follows the same pattern.

A valid match requires a bijection between letters in pattern and
non-empty words in s:
- Each letter maps to exactly one word
- Each word maps to exactly one letter

Approach:
----------
Split the input string s into words and first check whether the number
of words matches the length of the pattern.

Use a hash map to map each pattern character to its corresponding word,
and a set to track words that have already been mapped.
While iterating, if a pattern character has not been seen before, map it
only if the word is unused; if it has been seen, verify that it maps to
the same word.

If all characters satisfy these conditions, the pattern is followed.

Time Complexity:
----------------
O(n), where n is the number of words in s.
Each word is processed exactly once.

Space Complexity:
-----------------
O(n), due to the set storing mapped words.
(The pattern-to-word map is O(1) since pattern characters are limited to
26 lowercase English letters.)

"""

class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
strList = s.split(" ")
patternList = [char for char in pattern]

mappedCharSet = set()
patternMap = {}

if len(strList) != len(patternList):
return False

for i in range(len(strList)):
if patternList[i] not in patternMap:
if strList[i] in mappedCharSet:
return False
patternMap[patternList[i]] = strList[i]
mappedCharSet.add(strList[i])
if patternList[i] in patternMap:
if patternMap[patternList[i]] != strList[i]:
return False

return True
62 changes: 62 additions & 0 deletions leetcode49.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 9 20:07:23 2026

@author: rishigoswamy

Problem:
----------
https://leetcode.com/problems/group-anagrams/

Given an array of strings strs, group the anagrams together.
You can return the answer in any order.

An anagram is a word or phrase formed by rearranging the letters
of a different word or phrase, typically using all the original
letters exactly once.

Approach:
----------
To group anagrams efficiently, we use a frequency-based key
instead of sorting each word.

For every word, we create an array of size 26 that counts the
frequency of each character ('a' to 'z').
This frequency array uniquely represents the anagram group and
is converted to a tuple so it can be used as a dictionary key.

Words with identical character frequency arrays belong to the
same anagram group and are collected together using a hash map.

Time Complexity:
----------------
O(n * k), where n is the number of words and k is the maximum
length of a word.
Each word is processed once, and counting characters takes O(k).

Space Complexity:
-----------------
O(n * k), due to storing the frequency keys and grouped anagrams
in the hash map.

"""

import List
from collections import defaultdict

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
hMap = defaultdict(list)
res =[]

for word in strs:
charArray = [0] * 26
for char in word:
charArray[ord(char)-ord('a')]+=1
hMap[tuple(charArray)].append(word)

for key in hMap:
res.append(hMap[key])
return res