一天一道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. 2015 多校联赛 ——HDU5363(快速幂)

    Problem Description soda has a set S with n integers {1,2,…,n}. A set is called key set if the sum o ...

  2. [Russian Code Cup 2017 - Finals [Unofficial Mirror]]简要题解

    来自FallDream的博客,未经允许,请勿转载,谢谢. Div1难度+ACM赛制  和几个大佬组队逛了逛 A.给一个大小为n的集合ai(1<=ai<=1000000),要求你构造一个大小 ...

  3. bzoj1043[HAOI2008]下落的圆盘 计算几何

    1043: [HAOI2008]下落的圆盘 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1598  Solved: 676[Submit][Stat ...

  4. mysql 使用问题?

    linux中安装了mysql客户端和服务器端,为什么无法使用,总是报错呢 解决办法:使用dpkg -r mysql命令删除掉mysql-client和mysql-server了,还是不行,而且查看软件 ...

  5. 遗传算法:N皇后

    N皇后问题描述 N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 遗传算法 遗传算法是局部束搜索的变形: 与自 ...

  6. 判断是否是IE9浏览器的最短语句 var ie=!-[1,]

    没错,上面这个语句就可以判断浏览器是不是IE9以下的.why?1.[1,]在现代浏览器(ie包括ie9及以上)会被转换成[1], 而ie9以下就会转换成[1,undefined].2.分别对[1],和 ...

  7. EF实体的部分更新

    实现实体的部分更新假设实体InfoHotel如下: public class InfoHotel { public int Id{get;set;} public string Name{get;se ...

  8. 搭建一个交互式的前端构建环境.md

    为了提高开发效率.减少重复的操作,现在几乎全部的前端项目都需要依赖一些构建工具来实现自动化打包,主流的有webpack, gulp, grunt等.加上各种各样的配置文件就会形成了一个相对复杂的构建环 ...

  9. print语句中逗号(,)和反斜杠(\)的区别

    逗号结尾:   禁止输出换行反斜杠结尾:强制输出换行 >>> print ('A','B') #用一个逗号结尾就可以禁止输出换行 A B >>> print ('A ...

  10. 03_OGNL

    1.值栈: 解答Struts能够直接获取属性值: 原因:Struts并不是直接通过request隐式对象中获取,而是将整个对象封装到了ValueStack值栈中,直接匹配是否存在这个属性,找到了就取出 ...