LeetCode205. Isomorphic Strings
2016-01-24 12:51
337 查看
题目链接:
https://leetcode.com/problems/isomorphic-strings/题目描述:
判断两个字符串s,t是否同构。s中的字符能被t中对应字符替换。
For example,
Given “egg”, “add”, return true.
Given “foo”, “bar”, return false.
Given “paper”, “title”, return true.
题目分析:
这道题跟LeetCode290基本一样http://blog.csdn.net/codetz/article/details/50569138
建立两个map防止多对一情况,形成一一对应关系。
代码:
class Solution { public: bool isIsomorphic(string s, string t) { if(t.size()!=s.size()){ return false; } int len=s.size(); map<char,char> m1; map<char,char> m2; for(int i=0;i<len;i++){ if(m1.find(s[i])==m1.end() && m2.find(t[i])==m2.end()){ m1[s[i]]=t[i]; m2[t[i]]=s[i]; } else if(m1[s[i]]!=t[i] || m2[t[i]]!=s[i]){ return false; } } return true; } };
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- c语言实现hashmap(转载)
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- Ruby中Hash的11个问题解答
- PostgreSQL ERROR: invalid escape string 解决办法
- Ruby简明教程之数组和Hash介绍
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- C#中string和StingBuilder内存中的区别实例分析
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享
- asp.net String.IsNullOrEmpty 方法
- 在C#中生成与PHP一样的MD5 Hash Code的方法