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
33 changes: 33 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity : O(n * k log k) (n = number of words, k = avg length of word)
// Space Complexity : O(n * k)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just needed a common key to group same anagrams.


// Your code here along with comments explaining your approach in three sentences only
// For every word, we sort its characters and use that sorted string as a key.
// All words having same sorted key are anagrams, so we store them together in a map.
// Finally, we return all grouped lists from the map.

import java.util.*;

class Solution {
public List<List<String>> groupAnagrams(String[] strs) {

HashMap<String, List<String>> map = new HashMap<>();

for (String s : strs) {

char[] arr = s.toCharArray();
Arrays.sort(arr);
String key = new String(arr);

if (!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(s);
}

return new ArrayList<>(map.values());
}
}
34 changes: 34 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Time Complexity : O(n)
// Space Complexity : O(1) (fixed size arrays of 256)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, only had to make sure mapping works both ways.


// Your code here along with comments explaining your approach in three sentences only
// We keep a mapping from characters in s to characters in t and also from t to s.
// While scanning, if an existing mapping does not match the current characters, return false.
// If we finish without conflicts, then both strings are isomorphic.


class Solution {
public boolean isIsomorphic(String s, String t) {

int[] map1 = new int[256];
int[] map2 = new int[256];

for (int i = 0; i < s.length(); i++) {

char c1 = s.charAt(i);
char c2 = t.charAt(i);

if (map1[c1] == 0 && map2[c2] == 0) {
map1[c1] = c2;
map2[c2] = c1;
} else {
if (map1[c1] != c2 || map2[c2] != c1) return false;
}
}

return true;
}
}
39 changes: 39 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just needed to ensure one-to-one mapping (bijection).


// Your code here along with comments explaining your approach in three sentences only
// We split the string into words and check if number of words matches pattern length.
// Then we map each pattern character to one word and also map each word back to one character.
// If at any point mapping conflicts, return false, otherwise return true.

import java.util.*;

class Solution {
public boolean wordPattern(String pattern, String s) {

String[] words = s.split(" ");
if (pattern.length() != words.length) return false;

HashMap<Character, String> map1 = new HashMap<>();
HashMap<String, Character> map2 = new HashMap<>();

for (int i = 0; i < pattern.length(); i++) {

char ch = pattern.charAt(i);
String w = words[i];

if (!map1.containsKey(ch) && !map2.containsKey(w)) {
map1.put(ch, w);
map2.put(w, ch);
} else {
if (!map1.containsKey(ch) || !map2.containsKey(w)) return false;
if (!map1.get(ch).equals(w) || map2.get(w) != ch) return false;
}
}

return true;
}
}