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 ...
随机推荐
- python_04 基本数据类型、数字、字符串、列表、元组、字典
基本数据类型 所有的方法(函数)都带括号,且括号内没带等号的参数需传给它一个值,带等号的参数相当于有默认值 1.数字 int 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1 ...
- webstorm上svn的安装使用
1.首先要下载SlikSvn网址为:https://sliksvn.com/download/ 进入该网站可以根据需要下载32位的或者64位的svn.下图为要下载的图标样式 点击下载即可. 2.在w ...
- 小程序登录&授权&获取用户信息
一 .登录 时序图如下: wx.login() 获取js_code 示例代码: App({ onLaunch: function() { wx.login({ success: ...
- math模块
序号 方法 功能 示例 1 matd.ceil 取大于等于x的最小的整数值,如果x是一个整数,则返回x print(matd.ceil(10.1))# 11print(matd.ceil(-3.1)) ...
- MySQL5.7 查询用户,配置IP限制
1) MySQL 查询现在所有用户 select host,user from user; Navicat点击用户标签 查询;2) GRANT ALL PRIVILEGES ON *.* TO 'em ...
- 爬虫--Scrapy-基于RedisSpider实现的分布式爬虫
爬取网易新闻 需求:爬取的是基于文字的新闻数据(国内,国际,军事,航空) 先编写基于scrapycrawl 先创建工程 scrapy startproject 58Pro cd 58Pro 新建一个爬 ...
- Block 语法
Block,代码块,^符号是block的语法标记. 比如说,一个block的参数列表是一个UIView,返回值是个CGFloat,block名称是testBlock 可以定义为 CGFloat (^ ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position 1: ordinal not in range(128)
待研究: compressed_data = zlib.compress(json.dumps(data), 9) file_data = MySQLdb.escape_string(compress ...
- Linux命令:zip
语法: zip [选项] zip文件 源文件s 选项 全称 含义 举例 -r recursive 递归压缩子目录里的文件(包括子目录里的子目录) zip -r target.z ...
- Unable to locate Spring NamespaceHandler for XML schema namespace
1. 问题 本文将讨论Spring中最常见的配置问题 —— Spring的一个命名空间的名称空间处理程序没有找到. 大多数情况下,是由于一个特定的Spring的jar没有配置在classpath下,让 ...