[LeetCode]Isomorphic Strings
来源:互联网 发布:淘宝提醒我手机有木马 编辑:程序博客网 时间:2024/06/05 14:54
解题思路:
1,哈希表,字符串s 和 t 一一对应,令字符串s的字符为 key,遍历s,如果s不存在于hash表中,插入,并领其value为t对应位置的字符;如果s中某字符存在于hash表中,并且其value与t中对应位置的字符不同,则返回false,否则返回true;
2,上述过程要有个逆向的过程,即另建一个hash表,t的字符作为key,s的字符作为value,执行上述相同的逻辑
边界条件:s 和 t 的长度不等
前条件:创建hash表
不变式:遍历s中的字符c,在hash表中寻找c,如不存在,则插入;若存在,在比较value与t[i];
结束条件:发现hash表中存在字符c,其hash值与t中对应的字符不同,返回false;否则返回true
class Solution {public: bool isIsomorphic(string s, string t) { if (s.length() != t.length()){ return false; } unordered_map<char, char> hashT; unordered_map<char, char> hashTReverse; for (int i = 0; i < s.length(); ++i){ if (hashT.find(s[i]) != hashT.end() && hashT[s[i]] != t[i]) return false; if (hashTReverse.find(t[i]) != hashTReverse.end() && hashTReverse[t[i]] != s[i]) return false; hashT[s[i]] = t[i]; hashTReverse[t[i]] = s[i]; } return true; }};
0 0
- [LeetCode]Isomorphic Strings
- LeetCode 205 - Isomorphic Strings
- [LeetCode] Isomorphic Strings
- Isomorphic Strings Leetcode JAVA
- [LeetCode] Isomorphic Strings
- [LeetCode 205]Isomorphic Strings
- LeetCode 205-Isomorphic Strings
- leetcode Isomorphic Strings
- leetcode(c++) Isomorphic Strings
- leetcode--Isomorphic Strings
- [LeetCode]Isomorphic Strings
- LeetCode OJ Isomorphic Strings
- leetcode 205: Isomorphic Strings
- leetcode 205 Isomorphic Strings
- [leetcode] Isomorphic Strings
- 【leetcode】Isomorphic Strings
- [leetcode]Isomorphic Strings
- [LeetCode]Isomorphic Strings
- Python笔记——break的注意事项
- html5视频播放
- Android自定义按钮功能的一种实现思路
- 树懒_移动开学笔记__day02.1_web_css
- php-preg-1
- [LeetCode]Isomorphic Strings
- Android Studio学习笔记——构建系统
- BTrace实现浅析
- 一个小程序
- binbinyang---百度地图androidAPI Android SDK v3.5.0------------2.定位功能
- Java:输入年份和月份打印出相应的日历表
- linux中查看nginx、apache、php、mysql配置文件路径的方法
- QTP对Web页面上的文本框赋值的技术大全
- Linux程序设计的一些小工具使用