同构字符串
2015-08-06 22:02
218 查看
题目
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 and t,判断它们是否是同构字符串。同构字符串是指,s 中的字符可以被替换以得到 t。
字符串中同一个字符的所有出现位置必须被相同的字符替换,并且不改变其在原始字符串中的位置。不同的字符不能够被映射到相同的字符,但是一个字符可以映射到本身,例:
a , a
“egg”, “add”,
“foo”, “bar”,
“paper”, “title”,
我的解决办法是:通过hashMap构造s到t以及t到s的映射,然后在加上条件判断即可。代码如下(代码假设s与t等长):
bool isIsomorphic(string s, string t) { int aTob[256] = {0};//s到t的映射 int bToa[256] = {0};//t到s的映射 size_t len = s.size(); for(size_t i =0; i<len; ++i) { if(aTob[s[i]] == 0) aTob[s[i]] = t[i];//初始化aTob if(bToa[t[i]] == 0) bToa[t[i]] = s[i];//初始化bToa if(aTob[s[i]] != t[i] || bToa[t[i]] != s[i]) return false; } return true; }
相关文章推荐
- HDU - 1711 Number Sequence KMP字符串匹配
- 关于Java单例
- leetcode_Median of Two Sorted Arrays
- Puzzler: Nested computeIfAbsent -Java 8
- iOS 动画总结----UIView动画
- 第四章 初步进入linux世界
- ios代理的使用,正向传值,逆向传值
- CodeForces 567C. Geometric Progression(map 数学啊)
- 添加本地路由表项
- media响应式布局模版
- Day1_HTML_字体标签
- 20150806
- 学习BLAS库 -- DDOT
- JAVA技术交流群
- C语言-常用知识和技巧
- 设置线程的优先级
- codeforces 567c
- mysql 高可用6
- swift2.0学习之类型转换
- macbook Yosemite 10.10.4 编译Android 5.0