一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:给定两个字符串,s中的每个字符能个通过某种映射关系得到t,则返回true,否则返回false

解题思路:如题目给的例子,“egg”和“add”,e->a,g->d,通过这个映射关系可以得到add。

所以,很容易想到用hash表来解决这个问题。不过要注意: 不允许s中的两个不同字符对应t中的同一个字符。

即s = “ab”,t = “aa”,s中a和b不能同时对应t中的a

下面看代码:

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int len = s.length();
        if(len==0) return true;
        //必须要双向映射,避免出现一对多,多对一等情况
        char hash[256];
        char hash2[256];
        memset(hash,' ',256*sizeof(char));
        memset(hash2,' ',256*sizeof(char));
        for(int i = 0 ; i < len ; i++){
            if(hash[s[i]]==' '&&hash2[t[i]]==' '){//如果该组字符没有映射关系
                hash[s[i]] = t[i];//建立映射关系
                hash2[t[i]] = s[i];
            }
            else {
                if(hash[s[i]]==t[i]&&hash2[t[i]]==s[i]) continue;
                else return false;
            }
        }
        return true;
    }
};

【一天一道LeetCode】#205. Isomorphic Strings的更多相关文章

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

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  2. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

  3. LeetCode 205. Isomorphic Strings (同构字符串)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  4. Java for LeetCode 205 Isomorphic Strings

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  5. (easy)LeetCode 205.Isomorphic Strings (*)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  6. Java [Leetcode 205]Isomorphic Strings

    题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  7. [LeetCode] 205. Isomorphic Strings 解题思路 - Java

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. leetcode 205. Isomorphic Strings(哈希表)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

    翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...

  10. Leetcode 205 Isomorphic Strings 字符串处理

    判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...

随机推荐

  1. ●BZOJ 3238 [Ahoi2013]差异

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3238 题解: 后缀数组套路深. 问题转化为求出任意两个后缀的LCP之和 在计算贡献时,各种不 ...

  2. hdu 3433 A Task Process 二分+dp

    A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. [bzoj4245][ONTAK2015]OR-XOR

    来自FallDream的博客,未经允许,请勿转载,谢谢. 给定一个长度为n的序列a[1],a[2],...,a[n],请将它划分为m段连续的区间,设第i段的费用c[i]为该段内所有数字的异或和,则总费 ...

  4. BZOJ3052(树上带修莫队)

    树上莫队的基本思路是把树按dfs序分块,然后先按x所在块从小到大排序,再按y所在块从小到大排序,处理询问即可. 这道题带修改,再加一个时间维即可. 设块大小为T,那么时间复杂度为$O(nT+\frac ...

  5. SpringBoot学习之集成mybatis

    一.spring boot集成Mybatis gradle配置: //gradle配置: compile("org.springframework.boot:spring-boot-star ...

  6. Python中模块之logging & subprocess的讲解

    subprocess & logging模块的介绍 1. subprocess 该模块替代了os.system & os.pawn*所实现的功能. 2. logging 1. 日志五大 ...

  7. 【OCP|052】OCP最新题库解析(052)--小麦苗解答版

    [OCP|052]OCP最新题库解析(052)--小麦苗解答版 OCP最新题库解析历史连接(052):http://mp.weixin.qq.com/s/bUgn4-uciSndji_pUbLZfA ...

  8. display显示方式

    元素的diplay显示方式有多种,隐藏.块级.内联.内联-块级. 1.display:none   隐藏 2.display:block; 表示块级元素. 块级元素会自动在前面和后面加上换行,并且在其 ...

  9. Object 类

  10. img图片占不满整个div

    解决方法: img标签自带有3px的空隙,有很多解决方法第一种:设置img{font-size:0}第二种:设置img{display:block}第三种:设置img{vertical-align:t ...