[LeetCode]51. Ismorphic 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.

解法:同构字符串,就是说原字符串中的每个字符可由另外一个字符替代,可以被其本身替代,相同的字符一定要被同一个字符替代,且一个字符不能被多个字符替代,即不能出现一对多的映射。需要考虑两种情况:t中的相同字符映射到s中后是不同的字符("aa"-->"ab");t中的不同字符映射到s中后是相同的字符("ab"-->"aa")。因此设置两个Map,一个为t在s中的映射,另一个是s在t中的映射。后者是为了检查上面所说第二种情况。

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int n = s.size();
        vector<int> vt(256, -1); //t中字符在s中的映射
        vector<int> vs(256, -1); //s中字符在t中的映射
        for(int i = 0; i < n; ++i)
        {
            if(vt[t[i]] == -1) //t中当前字符还未映射过
            {
                if(vs[s[i]] == -1) //s中当前字符还未被映射过
                {
                    vt[t[i]] = s[i];
                    vs[s[i]] = t[i];
                }
                else if(vs[s[i]] != t[i]) return false; //two characters map to the same character
            }
            else if(s[i] != vt[t[i]]) return false; //a character map to two characters
        }
        return true;
    }
};

一种更简便的办法是:如果找到一对新的映射(s和t中的对应字符还没出现过),则将两个映射表的相应位置设为同一值,否则判断两个映射表的值是否相同即可。

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int n = s.size();
        vector<int> vt(256, -1); //t中字符在s中的映射
        vector<int> vs(256, -1); //s中字符在t中的映射
        for(int i = 0; i < n; ++i)
        {
            //t的当前字符已经在s中映射过或者s的当前字符已经在t中映射过,并且当前的映射和之前的映射不一致
            if(vt[t[i]] != vs[s[i]]) return false;
            //找到一对全新的映射
            vt[t[i]] = i;
            vs[s[i]] = i;
        }
        return true;
    }
};
时间: 2024-10-15 02:32:05

[LeetCode]51. Ismorphic Strings同构字符串的相关文章

[leetcode]205. Isomorphic Strings同构字符串

哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查另外一个map是不是也是对应位置重复 */ if (s.length()!=t.length()) { return false; } Map<Character,Integer> map1 = new HashMap<>(); Map<Character,Integer>

(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.

【LeetCode-面试算法经典-Java实现】【205-Isomorphic Strings(同构字符串)】

[205-Isomorphic Strings(同构字符串)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 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

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: 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 occurre

【LeetCode】- String to Integer (字符串转成整形)

[ 问题: ] Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). Y

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

POJ 2406 Power Strings (求字符串循环节出现的次数)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 44217   Accepted: 18449 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

205.同构字符串

同构字符串 给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字符上,但字符可以映射自己本身. 使用HashMap解决 class Solution { public boolean isIsomorphic(String s, String t) { if( s.length() != t.length() ) { return true; }

LeetCode OJ: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.