leetcode 205 Isomorphic Strings
2016-06-23 22:01
309 查看
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
return true.
Given
return false.
Given
return true.
Note:
You may assume both s and t have the same length.
Subscribe to see which companies asked this question
class Solution {
public:
bool isIsomorphic(string s, string t) {
vector<char> maps(255, 0);
vector<int> visited(255, 0);
for(int i = 0; i < s.length(); i++) {
if(maps[s[i]]!=0) {
if(maps[s[i]] != t[i]) return false;
} else {
if(visited[t[i]] == 1) return false;
maps[s[i]]=t[i];
visited[t[i]]=1;
}
}
return true;
}
}
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.
Subscribe to see which companies asked this question
class Solution {
public:
bool isIsomorphic(string s, string t) {
vector<char> maps(255, 0);
vector<int> visited(255, 0);
for(int i = 0; i < s.length(); i++) {
if(maps[s[i]]!=0) {
if(maps[s[i]] != t[i]) return false;
} else {
if(visited[t[i]] == 1) return false;
maps[s[i]]=t[i];
visited[t[i]]=1;
}
}
return true;
}
}
相关文章推荐
- SDWebImage原理
- LNMP环境搭建之--YUM仓库
- C++学习心得
- hash储存机制
- Harris角点检测原理及实现
- octave高画质图像处理字节数限制问
- 【Python学习日记】递归实现hanoi塔
- 解决:tomcat部署时deploy location不能显示加载后的路径【转载
- 思科基础命令大全
- [疯狂Java]面向对象:继承、覆盖、super
- java try...catch...finally机制详解
- requirejs讲解
- web 爬虫初体验
- 考后稍稍放松一波
- 堆排序
- BZOJ1083: [SCOI2005]繁忙的都市
- 莫比乌斯反演 bzoj 2301: [HAOI2011]Problem b模板
- 二叉树之统计叶子数
- 项目伊始如何入手
- tomcat+eclipse web应用开发环境配置