[Leetcode] 205. Isomorphic Strings 解题报告
来源:互联网 发布:网络性能管理系统 编辑:程序博客网 时间:2024/06/05 20:26
题目:
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.
思路:
哈希表的典型应用。需要注意的是,我们需要进行双向验证。否则对于aa和ba这样的例子而言,如果只从aa向ba验证,结果就不对了。
代码:
class Solution {public: bool isIsomorphic(string s, string t) { unordered_map<char, char> hash1, hash2; for (int i = 0; i < s.length(); ++i) { if (hash1.count(s[i]) && hash1[s[i]] != t[i]) { return false; } if (hash2.count(t[i]) && hash2[t[i]] != s[i]) { return false; } hash1[s[i]] = t[i], hash2[t[i]] = s[i]; } return true; }};
阅读全文
0 0
- [leetcode] 205. Isomorphic Strings 解题报告
- [Leetcode] 205. Isomorphic Strings 解题报告
- LeetCode-Isomorphic Strings-解题报告
- 【LeetCode】Isomorphic Strings 解题报告
- 【LeetCode】205.Isomorphic Strings
- [leetcode] 205.Isomorphic Strings
- LeetCode 205. Isomorphic Strings
- 205. Isomorphic Strings LeetCode
- 【LeetCode】205. Isomorphic Strings
- LeetCode *** 205. Isomorphic Strings
- leetcode 205. Isomorphic Strings
- LeetCode 205. Isomorphic Strings
- LeetCode 205. Isomorphic Strings
- leetcode-205. Isomorphic Strings
- LeetCode-205.Isomorphic Strings
- leetcode 205. Isomorphic Strings
- [leetcode] 205. Isomorphic Strings
- 205.[Leetcode]Isomorphic Strings
- DOS
- PHP常用的加密函数
- (日常打卡)自定义的一个模仿ArrayList的类, 你需要实现其中的add, get, remove , 等方法
- 腾讯 QQ for Linux 的安装及 tar.gz版本QQ 建立快捷方式
- 导出excel多种类对应工具类整理(util)
- [Leetcode] 205. Isomorphic Strings 解题报告
- Spring Boot 结合shiro做第三方登录验证
- C++网站开发MVC框架TreeFrog Framework教程——9.WebSocket
- BUG: scheduling while atomic经典bug
- STL-顺序容器-列表list
- Hibernate的五大核心接口
- 小程序如何接入微信支付?你可能会遇上这些坑
- 内部排序算法:冒泡排序
- C++中STL使用