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
53 changes: 53 additions & 0 deletions Leetcode205.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

// TC - O(n)
// SC - O(1) Since each dataset we created can have only 26 characters.
class Solution {
public boolean isIsomorphic(String s, String t) {
// Solution 1: Two HashMaps
// HashMap<Character, Character> sMap = new HashMap<>();
// HashMap<Character, Character> tMap = new HashMap<>();
// for (int i =0; i< s.length(); i++) {
// char sChar = s.charAt(i);
// char tChar = t.charAt(i);
// if(sMap.containsKey(sChar)) {
// if(sMap.get(sChar) != tChar) {
// return false;
// }
// } else {
// sMap.put(sChar, tChar);
// }

// if(tMap.containsKey(tChar)) {
// if(tMap.get(tChar) != sChar) {
// return false;
// }
// } else {
// tMap.put(tChar, sChar);
// }

// }

// return true;

// Solution 2: One HashMap and Hashset.
HashMap<Character, Character> sMap = new HashMap<>();
HashSet<Character> sSet = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!sMap.containsKey(c)) { // Character not present in the map
if (sSet.contains(t.charAt(i))) { // Character in t already mapped to some other character in s
return false;
}
sMap.put(c, t.charAt(i)); // adding {s Character: t Character} key value pair
sSet.add(t.charAt(i)); // adding t Character to the Hashset to indicate that it is already mapped to an
// s character.
} else {
if (sMap.get(c) != t.charAt(i)) { // character present but mapped to some other character
return false;
}
}
}
return true;

}
}
33 changes: 33 additions & 0 deletions Leetcode290.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
TC: O(n)
SC: O(1) Since max of 26 characters can be there in the pattern and each word can be mapped to only one character.
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] words = s.split("\\s");
if (words.length != pattern.length())
return false; // check if each word can be associated with a character or not. If not, return
// false since there's no 1:1 mapping possible.

HashMap<Character, String> charMap = new HashMap<>();
HashMap<String, Character> stringMap = new HashMap<>();
int k = 0;
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
if (charMap.get(c) == null) { // Character not present in the charMap
if (stringMap.get(words[i]) == null) { // String not present in the stringMap
charMap.put(c, words[i]); // Add {char: string} key value pair
stringMap.put(words[i], c);// Add {string: char} key value pair
} else { // String already mapped to some other character
return false;
}

} else {
if (!charMap.get(c).equals(words[i])) { // Character present but mapped to some other string
return false;
}
}
}
return true;
}
}
23 changes: 23 additions & 0 deletions Leetcode49.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// TC : O(n)
// SC : O(n)
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
// Solution 1: Checking the anagrams by sorting.
HashMap<String, List<String>> map = new HashMap<>();
for (int i = 0; i < strs.length; i++) {
String curr = strs[i];
// sort
char[] charArr = curr.toCharArray();
Arrays.sort(charArr);
String sorted = String.valueOf(charArr);

if (!map.containsKey(sorted)) {
map.put(sorted, new ArrayList<>());
}
List<String> li = map.get(sorted);
li.add(curr);
}
return new ArrayList<>(map.values());

}
}