Skip to content

Commit

Permalink
Time: 0 ms (100.00%), Space: 6.5 MB (53.35%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Jan 1, 2023
1 parent 4ca0006 commit 74b4f0b
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions 0290-word-pattern/0290-word-pattern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Solution {
public:
bool wordPattern(string pattern, string s) {
unordered_map<char,string> m;
unordered_map<string,char>mp;
vector<string> str;
string st="";
for(int i=0;i<s.size();i++)
{
if(s[i]==' ')
{
str.push_back(st);
st="";
}
else
{
st=st+s[i];
}
}

str.push_back(st);
if(pattern.size()!=str.size())return false;
for(int i=0;i<pattern.size();i++)
{
auto it=m.find(pattern[i]);
auto itr=mp.find(str[i]);
if(it==m.end())
{
if(itr==mp.end())
{
m[pattern[i]]=str[i];
mp[str[i]]=pattern[i];
}
else
{
return false;
}

}
else
{
if(it->second==str[i])continue;
else
return false;
}
}
return true;
}
};

0 comments on commit 74b4f0b

Please sign in to comment.