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

Hashing1 Complete #1895

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
30 changes: 30 additions & 0 deletions groupAna.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//we will assign prime no to each letters form a-z and then we will calculate the
//multiplication of each no if the multi is same then the string is an anargam and
//will move to that map else we will create new mapping for that index
//TC:0(nK) SC:O(n)
class Solution {
private:
double getHash(string&word){
double hash = 1;
int primes[26] = {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(auto&it:word){
int key = it-'a';
hash = hash*primes[key];
}
return hash;
}
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>>ans;
unordered_map<double,vector<string>>mpp;
for(auto&qt:strs){
double g = getHash(qt);
mpp[g].push_back(qt);
}
for(auto&ct:mpp){
ans.push_back(ct.second);
}
return ans;
}
};
29 changes: 29 additions & 0 deletions isoStrings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//use two maps to store mapping from both side
//Iterate tbrough string if maping is correct return true else false
//TC: O(n) SC O(n)
class Solution {
public:
bool isIsomorphic(string s, string t) {
unordered_map<char,char>smap;
unordered_map<char,char>tmap;
for(int i =0;i<s.size();i++){
char sVal = s[i];
char tVal = t[i];
if(smap[sVal]){
// e g g
// a d d
if(smap[sVal]!=tVal)return false;
}
else {smap[sVal] = tVal;}
}
for(int i =0;i<s.size();i++){
char sVal = s[i];
char tVal = t[i];
if(tmap[tVal]){
if(tmap[tVal]!=sVal)return false;
}
else {tmap[tVal] = sVal;}
}
return true;
}
};
42 changes: 42 additions & 0 deletions wordPattern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//TC:O(m+n)
//SC:O(m+n)
class Solution {
private:
string getWord(string&s,int& j){
string word;
while(j<s.size()&&s[j]!=' '){
word+=s[j];
j++;
}
j++;
return word;

}
public:
bool wordPattern(string pattern, string s) {
//declare two maps patteren to s & s to pattern
unordered_map<char,string>pts;
unordered_map<string,char>stp;
int j =0;
for(char c: pattern){
if(j>=s.size())return false;

string word = getWord(s,j);
//check if pattern is already in the map
auto it =pts.find(c);
//if it is already in the map and it is not mapped
// with the correctword return false
if(it!=pts.end() && it->second!=word){
return false;
}
//checking the second map that if it is also mapped with the same ele
auto qt = stp.find(word);
if(qt!=stp.end() && qt->second!=c) return false;
//
pts[c] = word;
stp[word] = c;
}
if(j>=s.size())return true;
return false;
}
};