LeetCode 205题:两个字符串是否同构,怎么改写为长尾?
- 内容介绍
- 文章标签
- 相关推荐
本文共计295个文字,预计阅读时间需要2分钟。
给定两个字符串 s 和 t,判断它们是否同构。两个字符串同构是指 s 中的字符可以通过替换得到 t。所有字符的出现必须替换为另一个字符,同时保持字符的顺序。
Given two stringssandt, determine if they are isomorphic.
Two strings are isomorphic if the characters inscan be replaced to gett.
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.
Example 1:
Input: s = t = Output: true"egg","add"
Example 2:
Input: s = t = Output: false"foo","bar"
Example 3:
Input: s = t =
Output: true
ASCII码有256个,维护两个数组,长度256,两个数组初始值相同,然后遍历两个string
"paper","title"
class Solution { public: bool isIsomorphic(string s, string t) { int m1[256] = {0};//ascii码共有256个 int m2[256] = {0}; for(int i = 0; i < s.size(); i++){ if(m1[s[i]] != m2[t[i]]) return false;//对应位置值不同,不符合 m1[s[i]] = i + 1; m2[t[i]] = i + 1; } return true; } };
本文共计295个文字,预计阅读时间需要2分钟。
给定两个字符串 s 和 t,判断它们是否同构。两个字符串同构是指 s 中的字符可以通过替换得到 t。所有字符的出现必须替换为另一个字符,同时保持字符的顺序。
Given two stringssandt, determine if they are isomorphic.
Two strings are isomorphic if the characters inscan be replaced to gett.
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.
Example 1:
Input: s = t = Output: true"egg","add"
Example 2:
Input: s = t = Output: false"foo","bar"
Example 3:
Input: s = t =
Output: true
ASCII码有256个,维护两个数组,长度256,两个数组初始值相同,然后遍历两个string
"paper","title"
class Solution { public: bool isIsomorphic(string s, string t) { int m1[256] = {0};//ascii码共有256个 int m2[256] = {0}; for(int i = 0; i < s.size(); i++){ if(m1[s[i]] != m2[t[i]]) return false;//对应位置值不同,不符合 m1[s[i]] = i + 1; m2[t[i]] = i + 1; } return true; } };

