LeetCode/205. Isomorphic Strings.md
2018-09-14 17:21:29 +08:00

28 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [205. Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/description/)
# 思路
题意就是判断两个字符串是否是同形的,重点就是理解对这个同形的意思。
若把字符串s中的某几种字符用另外某几种字符全部代替(只能是一一对应即全部的字符x都要变成同一个字符yx可以等于y)就变成字符串t则s和t就是同形的。
由此可见若将s和t中个每个字符都用一个数代替(这个数代表了该字符是第几个出现的如paper -> 12134, title -> 12134), 则结果应该是一样的。
为了记录是否出现过用map来实现此外还用一个count计数。
时间复杂度O(nlogn)
# C++
```
class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char, int>mp1, mp2;
int count = 1;
for(int i = 0; i < s.size(); i++){
if(mp1[s[i]] != mp2[t[i]]) return false; // 判断当前字符转变后是否相等
if(mp1[s[i]] == 0){
mp1[s[i]] = count;
mp2[t[i]] = count++;
}
}
return true;
}
};
```