题目: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.
设置两个cs和ct数组,记录目前遍历字符串s和t的下标i+1,如果两者不相等,返回false。
class Solution { public: bool isIsomorphic(string s, string t) { int cs[255] = {0}, ct[255] = {0}; for (int i = 0; i < s.size(); ++i){ if (cs[s[i]] != ct[t[i]]) return false; else{ cs[s[i]] = i + 1; ct[t[i]] = i + 1; } } return true; } };
时间: 2024-10-20 21:35:39