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
40 changes: 40 additions & 0 deletions grouping-anagrams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity: O(n × k) (n = number of words, k = average word length)
// Space Complexity: O(n)
// Explantion: We assign a unique prime number to each character from 'a' to 'z'. For every word, we multiply the prime values of its characters to generate a unique BigInteger key. Since anagrams produce the same prime product, we group words in a HashMap using this key. Finally, we return all grouped anagrams from the map.
import java.math.BigInteger;
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
int m = strs.length;
if (m==0) return new ArrayList<>();
if(m==1) {
return Arrays.asList(Arrays.asList(strs[0]));
}
Map<BigInteger, List<String>> map = new HashMap<>();
int[] primes = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101
};
for (int i=0; i<m; i++) {
String curr = strs[i];
int currLength = curr.length();
BigInteger multi = BigInteger.valueOf(1);
for (int j =0; j<currLength; j++) {
char s = curr.charAt(j);
multi = multi.multiply(BigInteger.valueOf(primes[s - 'a']));
}
List<String> append;
if(!map.containsKey(multi)) {
append = new ArrayList();
} else {
append = map.get(multi);
}
append.add(curr);
map.put(multi, append);
}

return new ArrayList<>(map.values());


}
}
30 changes: 30 additions & 0 deletions isomorphic-strings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// TimeComplexity: O(n)
// SpaceComplexity: O(1)
// Explnation : We first check if both strings have the same length. Then we use two HashMaps to maintain a one-to-one mapping between characters of s and t. While iterating, we verify that any existing mapping is consistent; otherwise, we return false. If all characters follow a valid bijection, we return true.
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> map1 = new HashMap<>();
Map<Character, Character> map2 = new HashMap<>();
int m = s.length();
int n = t.length();
if(m!=n) return false;
for(int i=0; i<m; i++) {
if(map1.containsKey(s.charAt(i))) {
if(map1.get(s.charAt(i)) != t.charAt(i)) {
return false;
}
} else{
map1.put(s.charAt(i), t.charAt(i));
}
if(map2.containsKey(t.charAt(i))) {
if(map2.get(t.charAt(i)) != s.charAt(i)) {
return false;
}
} else{
map2.put(t.charAt(i), s.charAt(i));
}
}
return true;

}
}
36 changes: 36 additions & 0 deletions word-pattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

// TimeComplexity: O(n) where n is length of the pattern
// SpceComplexity: O(n)
// Explanation: Split the string into words. If pattern length != number of words, return false. We use two HashMaps to maintain bijection. While iterating: If mapping exists, verify consistency. If not, create new mapping. If all checks pass, return true.
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] words = s.split(" ");

Map<Character, String> map1 = new HashMap<>();
Map<String, Character> map2 = new HashMap<>();
int m = pattern.length();
int n = words.length;
if(m!=n) return false;
for(int i=0; i<pattern.length(); i++) {
char curr = pattern.charAt(i);
String word = words[i];

if(map1.containsKey(curr)) {
if(!map1.get(curr).equals(word) ) {
return false;
}
} else{
map1.put(curr, word);
}
if(map2.containsKey(word)) {
if(map2.get(word) != curr) {
return false;
}
} else{
map2.put(word, curr);
}
}
return true;

}
}