-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
rajata-ks
wants to merge
1
commit into
super30admin:master
Choose a base branch
from
rajata-ks:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Completed Hashing-1 #1886
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.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)); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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