mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
Create 205. Isomorphic Strings.md
This commit is contained in:
parent
a5fa6080ae
commit
292f50edad
27
205. Isomorphic Strings.md
Normal file
27
205. Isomorphic Strings.md
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# [205. Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/description/)
|
||||||
|
# 思路
|
||||||
|
题意就是判断两个字符串是否是同形的,重点就是理解对这个同形的意思。
|
||||||
|
若把字符串s中的某几种字符用另外某几种字符全部代替(只能是一一对应,即全部的字符x都要变成同一个字符y,x可以等于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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user