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 ...
随机推荐
- leetcode997
class Solution: def findJudge(self, N: int, trust: 'List[List[int]]') -> int: if N==1 and len(tru ...
- 浅谈JSONObject与JSONArray的区别
例如:一个json字符串如下: { "type":[{"a","1"},{"a","2"},{&qu ...
- redis 学习笔记1(安装以及控制台命令)
为什么要学习这个? 分布式技术必会,得益于redis的设计理念,内存数据库,epoll(多路复用)模型,单线程模型除去了锁和上下文切换,提高了性能.单线程保证执行顺序(轮询),在分布式环境下对于数据的 ...
- golang使用Nsq(转)
为什么要使用Nsq 最近一直在寻找一个高性能,高可用的消息队列做内部服务之间的通讯.一开始想到用zeromq,但在查找资料的过程中,意外的发现了Nsq这个由golang开发的消息队列,毕竟是golan ...
- 一个Tparams小测试
var aParams: TParams; aPar: TParam; I:Integer; begin aParams := TParams.Create(nil); aPar := aParams ...
- python语言中的数据类型之集合
数据类型 集合类型 set 用途:1.关系运算 2.去重 定义方式:在{}内用逗号分隔开多个元素,但元素的特点是 1.集合内元素必须是不可变类型 2.集合内元素无序 集合内元素不能 ...
- FlashDevelop关闭分号自动格式化
菜单Tools-Program Settings-ASCompletion-Helpers-Characters Requiring Whitespace 清空",;*+-=/%<&g ...
- Oracle快速导入数据工具
sqlldr是oracle自带的快速导入批量数据的工具,常用于性能测试.考虑手工构造控制文件较为繁琐,因此使用脚本完成批量数据的自动导入. 基本知识 sqlldr命令语法 sqlldr dbname/ ...
- 关于openwrt使用web升级提示固件版本不对的处理方法
参考资料:https://blog.csdn.net/caoshunxin01/article/details/79355602 当openwrt使用web升级提示固件版本不对: The upload ...
- JAVAWEB 一一 Spirng(框架,IOC控制反转,DI依赖注入)
jar包 applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <bea ...