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 Completed #1889

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
8 changes: 8 additions & 0 deletions .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 .idea/Hashing-1.iml

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

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

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

47 changes: 47 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//Problem 1:
//Given an array of strings, group anagrams together.
//
// Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ]
//
//Note: All inputs will be in lowercase. The order of your output does not matter.

//Time complexity: O(n*k)
//Space complexity: O(n*k)

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

public class problem1 {

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

for (String word: strs)
{
double hash= getHash(word);
if (!map.containsKey(hash))
{
map.put(hash, new ArrayList<>());
}
map.get(hash).add(word);
}
return new ArrayList<>(map.values());
}
private double getHash(String word)
{
double hash=1;
int[] prime= {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,61,67,71,73,79,83,89,97,100};
for (char c: word.toCharArray())
{
hash = hash * prime[c - 'a'];
}
return hash;
}

public static void main(String[] args)
{

}
}
61 changes: 61 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
//
//Example 1: Input: s = "egg", t = "add" Output: true
//
//Example 2: Input: s = "foo", t = "bar" Output: false
//
//Example 3: Input: s = "paper", t = "title" Output: true Note: You may assume both s and t have the same length.

//Time complexity: O(n)
//Space complexity: O(n)


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

public class problem2 {
private static boolean isIsomorphic(String a, String b)
{
if (a==null && b==null)
{
return true;
}
if(a==null || b==null)
{
return false;
}
if (a.length() != b.length())
{
return false;
}
HashMap<Character,Character> sMap= new HashMap<>();
HashSet<Character> sSet= new HashSet<>();
for (int i=0;i<a.length();i++)
{
char aChar= a.charAt(i);
char bChar= b.charAt(i);

if (sMap.containsKey(aChar))
{
if (sMap.get(aChar) != bChar)
{
return false;
}
}
else {
if (sSet.contains(bChar)) // Search space is O(1)
{
return false;
}
sSet.add(bChar);
sMap.put(aChar, bChar);

}
}
return true;
}
public static void main(String[] args)
{
isIsomorphic("egg","add");
}
}
69 changes: 69 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//Problem 3:
//Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
//
//Example 1: Input: pattern = "abba", str = "dog cat cat dog" Output: true
//
//Example 2: Input:pattern = "abba", str = "dog cat cat fish" Output: false
//
//Example 3: Input: pattern = "aaaa", str = "dog cat cat dog" Output: false
//
//Example 4: Input: pattern = "abba", str = "dog dog dog dog" Output: false Notes: You may assume pattern contains only lowercase letters, and str contains lowercase letters that may be separated by a single space.


//Time complexity: O(n)
//Space complexity: O(n)


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

public class problem3 {

public static boolean isIsomorphic(String pattern, String s)
{
if (pattern==null && s==null)
{
return true;
}
if (pattern==null && s==null)
{
return false;
}
String[] words=s.split(" ");
int wordCount=words.length;
if (pattern.length()!=wordCount)
{
return false;
}
HashMap<Character, String> map=new HashMap<>();
String[] str=s.split(" ");
HashSet<String> set= new HashSet<>();
for (int i=0;i<pattern.length();i++)
{
char a= pattern.charAt(i);
String b=words[i];
if (map.containsKey(a))
{
if (!map.get(a).equals(b))
{
return false;
}
}
else {
if (set.contains(b))
{
return false;
}
set.add(b);
map.put(a,b);
}
}
return true;
}
public static void main(String[] args)
{
boolean res= isIsomorphic("egg","add");
}


}