205. 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. 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 中的字符。不能一对多,也不能多对一。

利用两个map实现。

代码如下:

 class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char, char> mapA;
map<char, char> mapB;
int n = s.length();
for(int i = ; i < n; i++)
{
char ss = s[i];
char tt = t[i];
map<char, char>::iterator it = mapA.find(ss);
if(it != mapA.end())
{
if(mapA[ss] != tt)
{
return false;
}
}
else
{
map<char, char>::iterator ii = mapB.find(tt);
if(ii != mapB.end() && mapB[tt] != ss)
{
return false;
}
else
{
mapA[ss] = tt;
mapB[tt] = ss;
}
}
}
return true;
}
};

最新文章

  1. TortoiseSVN 合并操作简明教程
  2. window配置nginx+php+mysql
  3. js String对象
  4. Linux内核设计第七周 ——可执行程序的装载
  5. 【转】SVN服务器客户端以及环境的搭建和使用
  6. linux软件的安装,更新与卸载
  7. linux 打补丁
  8. Android中ListView分页加载数据
  9. mysql读写分离
  10. JavaScript入门之数组:Array类型详解
  11. 学习总结:工程管理与makefile
  12. Azkaban-2.5及Plugins的安装配置
  13. 值类型和引用类型的区别,struct和class的区别
  14. 谈一谈对MySQL InnoDB的认识及数据库事物处理的隔离级别
  15. discuz 文件模板edit
  16. Mapreduce运行过程分析(基于Hadoop2.4)——(二)
  17. C# Language Specification 5.0 (翻译)第六章 转换
  18. 【HDU 3590】 PP and QQ (博弈-Anti-SG游戏,SJ定理,树上删边游戏)
  19. Notes of Daily Scrum Meeting(11.8)
  20. 1009 Product of Polynomials (25)(25 point(s))

热门文章

  1. 1.5 linux笔记
  2. Python非阻塞网络通信Howto
  3. web编程
  4. JS Date当前时间:获取日期时间方法在各浏览器中的差异
  5. [转]libevent简介和使用
  6. System.Data.OleDb操作access数据库类,【bubuko.com】
  7. Android学习笔记——权限解释
  8. [系统开发] 基于Ansible的产品上线系统
  9. [经验交流] Mesos-dns 和 Marathon-lb 简要使用方法
  10. 彻底解决rman恢复碰到ora-01152错