205. Isomorphic Strings (Map)
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.
思路: 注意本题是一一对应的关系,而hashmap只能保证索引值唯一,即s到t是唯一的,但是可能存在2个s对应同一个t,所以增加了set加以判断
class Solution {
public:
bool isIsomorphic(string s, string t) {
int len = s.length();
if(len == ) return true; map<char,char> c_map;
set<char> c_set; //to avoid two character in s maps to the same character in t
for(int i = ; i < len; i++){
if(c_map.find(s[i]) == c_map.end()){
if(c_set.find(t[i]) == c_set.end()){
c_map[s[i]] = t[i];
c_set.insert(t[i]);
}
else return false;
}
else if(c_map[s[i]]!=t[i]) return false;
}
return true;
}
};
205. Isomorphic Strings (Map)的更多相关文章
- 205. Isomorphic Strings - LeetCode
Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【刷题-LeetCode】205. Isomorphic Strings
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
- *205. Isomorphic Strings (string & map idea)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- (String) 205.Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- Redis 常见配置
- react-native android 和ios 集成 jpush-react-native 激光推送
安装 $ npm install jpush-react-native --save # jpush-react-native 版本以后需要同时安装 jcore-react-native $ npm ...
- cxgrid合并值相同的某列
设置 cxGrid 的某列的 CellMerging 属性可使这一列相同值的单元格合并. 1)cxGridDBTableViewColumn1.Options.CellMerging:=true 2 ...
- Win7下npm命令Error: ENOENT问题解决
Win7下在执行npm命令,比如npm list时出现下面错误:
- 安装安卓SDK和JDK的简便方法
直接在VS的安装程序里选:使用.NET的移动开发,其中就包括了安卓SDK,JAVA SE等 另外:自己手动安装SDK时,不要选模拟器相关的东西,太大了,如果每个版本都选,安装下来上100G以上
- oracle第四天笔记
游标 /* 序列: ORACLE使用来模拟ID自动增长的 */ create sequence seq_test4; create table test2( tid number primary ke ...
- WebStorm新创建项目介绍
WebStorm创建一个项目 这里支持有很多的类型项目: Empty Project ----一个空的项目 Html5 Boilerplate ----HTML5开发框架 We ...
- HTTPS好文推荐
认真看完这几篇文章,HTTPS相关内容应该就能大概了解了. 1.https(ssl)协议以及wireshark抓包分析与解密 2.数字证书原理 3.也许,这样理解HTTPS更容易 4.SSL/TLS原 ...
- 10.mysql-触发器.md
目录 定义 语法 定义 当操作了某张表时,希望同时触发一些动作/行为,可以使用触发器完成 语法 -- 需求: 当向员工表插入一条记录时,希望mysql自动同时往日志表插入数据 -- 创建触发器(添加) ...
- Python模块和类.md
模块的定义 代码的层次结构 对于python的层次结构一般为包->模块 包也就是文件夹,但是文件夹下必须有文件"init.py"那么此文件夹才可以被识别为包."in ...