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
87 changes: 87 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions GroupAnagrams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class GroupAnagrams {
public List<List<String>> groupAnagrams(String[] strs){
HashMap<String, List<String>> map = new HashMap<>();
for(int i=0;i<strs.length;i++){
String curr=strs[i];
//sorting character array and get String back
char [] charArr = curr.toCharArray(); ////Breaks the string "eat" into individual characters and stores in charArr -- ['e', 'a', 't']
Arrays.sort(charArr); //'e', 'a', 't'} → {'a', 'e', 't'}
String sorted = String.valueOf(charArr); //converts character array to string "aet"
//if map contains sorted, if not then add sorted string initiate with a new arraylist
if(!map.containsKey(sorted)){
map.put(sorted, new ArrayList<>()); //map = {"aet" : []} creates new empty list
}
//get list of all Strings
List<String> li = map.get(sorted);
li.add(curr); //add to list
map.put(sorted,li);//add to map
}
return new ArrayList<>(map.values());
}
}

//TC: O(N KlogK)
//SC: O(NK)
42 changes: 42 additions & 0 deletions IsIsomorphic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.HashMap;

public class IsIsomorphic {
public static void main(String args[]){
String s = "paper";
String t = "title";
System.out.println(isIsomorphic(s,t));
}
public static boolean isIsomorphic(String s, String t) {
int sl = s.length();
int tl = t.length();
//create two HashMaps to store characters of s & t
HashMap<Character, Character> sMap = new HashMap<>();
HashMap<Character, Character> tMap = new HashMap<>();
for(int i=0; i < sl; i++){
char sChar = s.charAt(i);
char tChar = t.charAt(i);
//s to t mapping
if(sMap.containsKey(sChar)){
//if earlier mapped character of s is equal to t, if not return false --> breech
if(sMap.get(sChar) != tChar){
return false;
}
} else{
sMap.put(sChar, tChar);
}
// t to s mapping
if(tMap.containsKey(tChar)){
//if earlier mapped character of t is equal to s, if not return false --> breech
if(tMap.get(tChar) != sChar){
return false;
}
} else{
tMap.put(tChar, sChar);
}
}
return true;
}
}

// TC : O(N)
//SC : O(1)
3 changes: 3 additions & 0 deletions out/production/Hashing-1/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions out/production/Hashing-1/.idea/Hashing-1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions out/production/Hashing-1/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions out/production/Hashing-1/.idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions out/production/Hashing-1/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions out/production/Hashing-1/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions out/production/Hashing-1/.idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions out/production/Hashing-1/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added out/production/Hashing-1/IsIsomorphic.class
Binary file not shown.
Loading