jayati

Isomorphic Strings

Sep 20th, 2022
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool isIsomorphic(string s, string t) {
  4. unordered_map<char,char> m;
  5. unordered_map<char,char> m2;
  6. bool ans=true;
  7. for(int i=0;i<s.length();i++)
  8. {
  9. if (m.find(s[i]) != m.end())
  10. {
  11. if(m[s[i]]!=t[i])
  12. {
  13. ans=false;
  14. break;
  15. }
  16.  
  17. }
  18. else
  19. {
  20. m[s[i]]=t[i];
  21. }
  22.  
  23. }
  24. for(int i=0;i<s.length();i++)
  25. {
  26. if (m2.find(t[i]) != m2.end())
  27. {
  28. if(m2[t[i]]!=s[i])
  29. {
  30. ans=false;
  31. break;
  32. }
  33.  
  34. }
  35. else
  36. {
  37. m2[t[i]]=s[i];
  38. }
  39.  
  40. }
  41. return ans;
  42. }
  43. };
Advertisement
Add Comment
Please, Sign In to add comment