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

Completed Hashing-1 #1886

Open
wants to merge 1 commit 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
41 changes: 41 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Time Complexity :isIsomorphic: O(2n)=O(n) , n is number of characters in the string
// Space Complexity :0(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : nothing

import java.util.HashMap;
import java.util.HashSet;

public class Problem1 {
public boolean isIsomorphic(String s, String t) {
if(s==null && t==null) return true;
if(s==null || t==null) return false;
if(s.length() != t.length()) return false;


HashMap<Character, Character> sMap= new HashMap<>();
HashSet<Character> set = new HashSet<>();

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{
if(set.contains(tChar)) return false;
set.add(tChar);
sMap.put(sChar,tChar);
}
}
return true;
}


public static void main(String args[]) {
Problem1 problem1 = new Problem1();
String s= "bba";
String t= "aad";
System.out.println("isomorphic "+problem1.isIsomorphic(s,t));

}
}
41 changes: 41 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Time Complexity :isIsomorphic: O(2n)=O(n) , n is number of characters in the string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update time and space complexity

// Space Complexity :0(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : nothing

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Problem2 {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<Double, List<String>> map = new HashMap<>();

for (String str : strs){
double hash = getHash(str);
if(!map.containsKey(hash)){
map.put(hash,new ArrayList<>());
}
map.get(hash).add(str);

}

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

private double getHash(String str){
int hash = 1;
int[] prime ={ 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( char c : str.toCharArray()){
hash = hash * prime[c-'a'];
}
return hash;
}

public static void main(String args[]) {
Problem2 problem2= new Problem2();
String[] s= {"eat","tea","tan","ate","nat","bat"};
System.out.println("grouped anagram is "+problem2.groupAnagrams(s));

}
}
36 changes: 36 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Time Complexity :wordPattern: O(2n)=O(n) , n is number of characters in the string
// Space Complexity :0(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : nothing

import java.util.HashMap;
import java.util.HashSet;

public class Problem3 {
public boolean wordPattern(String pattern, String s) {
String[] words = s.split(" ");
if(pattern.length() != words.length) return false;
HashMap<Character,String> map= new HashMap<>();
HashSet<String> set = new HashSet<>();

for(int i=0; i<pattern.length(); i++){
char pChar = pattern.charAt(i);
if(map.containsKey(pChar)){
if(!map.get(pChar).equals(words[i])) return false;
} else{
if(set.contains(words[i])) return false;
set.add(words[i]);
map.put(pChar,words[i]);
}
}
return true;
}

public static void main(String args[]) {
Problem3 problem3= new Problem3();
String pattern= "abba";
String s= "dog dog dog dog";
System.out.println("pattern match found "+problem3.wordPattern(pattern,s));

}
}