LeetCode Isomorphic Strings
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.
For example,
Given "egg"
, "add"
, return true.
Given "foo"
, "bar"
, return false.
Given "paper"
, "title"
, return true.
Note:
You may assume both s and t have the same length.
1 // 08:18 2 class Solution { 3 public: 4 bool isIsomorphic(string s, string t) { 5 int slen = s.size(); 6 int tlen = t.size(); 7 if (slen != tlen) { 8 return false; 9 } 10 char tbl[256] = {0}; 11 bool used[256] = {0}; 12 for (int i=0; i<slen; i++) { 13 char ch = s[i]; 14 if (tbl[ch] == 0 && !used[t[i]]) { 15 tbl[ch] = t[i]; 16 used[t[i]] = true; 17 continue; 18 } 19 if (tbl[ch] != t[i]) { 20 return false; 21 } 22 } 23 return true; 24 } 25 };