Isomorphic String
2015-08-19 00:47
369 查看
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
Given
Given
Note:
You may assume both s and t have the same length.
Analyse:
1. map twice.
Runtime: 68ms.
View Code
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.
Analyse:
1. map twice.
Runtime: 68ms.
class Solution { public: bool isIsomorphic(string s, string t) { if(s.length() == 0) return true; map<char, char> m; m[s[0]] = t[0]; map<char, char> n; n[t[0]] = s[0]; for(int i = 1; i < s.length(); i++){ if(m.find(s[i]) != m.end() && m.find(s[i])->second != t[i] || n.find(t[i]) != n.end() && n.find(t[i])->second != s[i]) return false; else{ m.insert(pair<char, char> (s[i], t[i])); n.insert(pair<char, char> (t[i], s[i])); } } return true; } };
View Code
相关文章推荐
- OC对象中拥有对象
- 数论基础的补充讲解
- 常用排序算法
- 黑马程序员——Java基础IO流概述、字符流、字节流、流操作规律、File类、Properties类、打印流、序列流
- 软件项目实战及文档编写(评审)技巧
- 137. Single Number II
- 1091. Acute Stroke (30)
- [LeetCode] Single Number III 单独的数字之三
- JMX监控Zookeeper状态Java API
- Linux常用命令(一)-管理文件和目录命令(3)-cd命令
- linux下的挂载点和分区是什么关系
- HTTP状态码详解
- 一个资深系统管理员的O2O实践(三)
- 一个资深系统管理员的O2O实践(三)
- 一个资深系统管理员的O2O实践(三)
- 纯净版Ubuntu系统安装web服务器需要安装的库
- 学习新东西的动力
- BA-BACnet对象
- hdu4474 bfs搜索
- BA-siemens-insight_designer不支持win7 64位操作系统