Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hashing-1 Solved #1888

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
39 changes: 39 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//https://leetcode.com/problems/group-anagrams/
//Time Complexity O(N)
//Space Complexity O(N)

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

Map<Double, List<String>> anagrams = new HashMap<>();

for(String str :strs){

Double hashVal = getHash(str);
List<String> anaList = anagrams.getOrDefault(hashVal, new ArrayList<>());
anaList.add(str);
anagrams.put(hashVal,anaList);

}

return new ArrayList<>(anagrams.values());
}


Double getHash(String input){
int[] primeArray = new int[] {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};
double hash = 1;
char[] inputChars = input.toCharArray();
for(char c : inputChars){
hash = hash * primeArray[c-'a'];
}

return hash;

}
}
35 changes: 35 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.HashMap;

//https://leetcode.com/problems/isomorphic-strings/
//Time complexity O(N)
//Space complexity O(1) as space could not be more than the character set
public class Problem2 {
public boolean isIsomorphic(String s, String t) {

if(s.length() != t.length()){
return false;
}
HashMap<Character,Character> st1 = new HashMap<>();
HashMap<Character,Character> st2 = new HashMap<>();

for(int i=0;i<s.length();i++){
char sc = s.charAt(i);
char tc = t.charAt(i);
if(!st1.containsKey(sc)){
st1.put(sc,tc);
}else{
if(!st1.get(sc).equals(tc)){
return false;
}
}
if(!st2.containsKey(tc)){
st2.put(tc,sc);
}else{
if(!st2.get(tc).equals(sc)){
return false;
}
}
}
return true;
}
}
42 changes: 42 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.HashMap;
import java.util.Map;

//https://leetcode.com/problems/word-pattern/description/
//Time complexity O(N)
//Space complexity O(N)
public class Problem3 {
public boolean wordPattern(String pattern, String s) {
Map<Character,String> map = new HashMap<>();
Map<String,Character> strMap = new HashMap<>();
char[] patternChar = pattern.toCharArray();
String[] strArr = s.split(" ");
if(patternChar.length !=strArr.length ){
return false;
}
for(int i=0;i<patternChar.length;i++){
if(map.containsKey(patternChar[i])){
if(!map.get(patternChar[i]).equals(strArr[i])){
return false;
}
}else{
map.put(patternChar[i],strArr[i]);


}


if(strMap.containsKey(strArr[i])){
if(!strMap.get(strArr[i]).equals(patternChar[i])){
return false;
}
}else{
strMap.put(strArr[i],patternChar[i]);


}

}
return true;

}
}