fork download
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5. class Solution {
  6. public:
  7. bool isIsomorphic(string s, string t) {
  8.  
  9. if(s.size() != t.size())
  10. return false;
  11.  
  12. map<char, int> ms;
  13. map<char, int> mt;
  14.  
  15. bool first = true;
  16. for(int i = 0; i <= s.size(); i++)
  17. {
  18. cout << i << " " << s[i] << ' ' << t[i] << endl;
  19. if(ms[s[i]] != mt[t[i]])
  20. {
  21. first = false;
  22. break;
  23. }
  24. else
  25. ms[s[i]] = mt[t[i]] = i;
  26. }
  27. cout << "first " << first << endl;
  28.  
  29. ms.clear();
  30. mt.clear();
  31.  
  32. int n = s.size();
  33. int i = n;
  34.  
  35. bool second = true;
  36. for (; i >= 0 ; --i) {
  37. if (ms[s[n-i]] != mt[t[n-i]])/*|| s[i] == t[i]*/
  38. {
  39. second = false;
  40. break;
  41. }
  42. else
  43. ms[s[n-i]] = mt[t[n-i]] = i;
  44. }
  45. cout << "second " << second << endl;
  46. return second;
  47. }
  48. };
  49.  
  50. int main() {
  51. Solution sol;
  52. //sol.isIsomorphic("egg", "add");
  53. //sol.isIsomorphic("foo", "bar");
  54. //sol.isIsomorphic("paper", "title");
  55. sol.isIsomorphic("aa", "ab");
  56. return 0;
  57. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
0 a a
1 a b
2  
first 1
second 0