Isomorphic Strings
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.
这题需要注意的地方是我们要从S到T 和T到S都检查一遍。
public class Solution {
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) return false;
Map<Character, Character> map = new HashMap<>();
for (int i = ; i < s.length(); i++) {
char chKey = s.charAt(i);
char chValue = t.charAt(i);
if(map.containsKey(chKey)) {
char value = map.get(chKey);
if (value != chValue) {
return false;
}
} else {
map.put(chKey, chValue);
}
}
map.clear();
for (int i = ; i < s.length(); i++) {
char chKey = t.charAt(i);
char chValue = s.charAt(i);
if(map.containsKey(chKey)) {
char value = map.get(chKey);
if (value != chValue) {
return false;
}
} else {
map.put(chKey, chValue);
}
}
return true;
}
}
Isomorphic Strings的更多相关文章
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
- leetcode:Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Codeforces 985 F - Isomorphic Strings
F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...
- Educational Codeforces Round 44 (Rated for Div. 2) F - Isomorphic Strings
F - Isomorphic Strings 题目大意:给你一个长度为n 由小写字母组成的字符串,有m个询问, 每个询问给你两个区间, 问你xi,yi能不能形成映射关系. 思路:这个题意好难懂啊... ...
- CodeForces985F -- Isomorphic Strings
F. Isomorphic Strings time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- LeetCode_205. Isomorphic Strings
205. Isomorphic Strings Easy Given two strings s and t, determine if they are isomorphic. Two string ...
- 【刷题-LeetCode】205. Isomorphic Strings
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
随机推荐
- HTML 简介
什么是HTML HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言: Hyper Text Markup Language HTML 不是一种编程语言,而是一种标记语言 标记语言是一 ...
- onscroll事件的浏览器支持
window和普通div对象的scroll事件,被全部浏览器支持,其他元素的scroll事件,仅部分浏览器支持,如下图 出处: http://w3help.org/zh-cn/causes/SD901 ...
- ASP.NET MVC使用Bootstrap系列(4)——使用JavaScript插件
阅读目录 序言 Data属性 VS 编程API 下拉菜单(dropdown.js) 模态框(modal.js) 标签页(tab.js) 工具提示(tooltip.js) 弹出框(popover.js) ...
- MySQL 使用SELECT ... FOR UPDATE 做事务写入前的确认(转)
Select…For Update语句的语法与select语句相同,只是在select语句的后面加FOR UPDATE [NOWAIT]子句. 该语句用来锁定特定的行(如果有where子句,就是满足w ...
- CxImage
启动项目的时候显示此时 百度“无法启动程序cximage.lib” 得到http://tieba.baidu.com/p/1935208210把第二项设为启动项即可 为什么设置第二项为启动项呢 因为h ...
- Gulp实现web服务器
Gulp实现web服务器 阅读目录 一:gulp实现web服务器配置: 二:添加实时刷新(livereload)支持 回到顶部 一:gulp实现web服务器配置: 对于前端开发而言,需要在本地搭建一个 ...
- 【Solr】Solr的安装部署
目录 Solr安装部署 Solr Web界面分析 回到顶部 solr安装和部署 solr下载 http://lucene.apache.org/ 安装solr,就是去部署它的war包,war包所在的位 ...
- 全栈工程师是不是必须得会HTML5?
最近看了很多招聘,都要求前端工程师必须得会HTML5.个人觉得只要会了html,HTML5不会太难,不过全栈工程师要求会的太多了 HTML CSS JavaScript HTML 5 CSS 3 都说 ...
- WP8微信5.3开始内测 支持Cortana语音 两微破冰了?
WP版微信v5.3内测版昨发布了,进行了一些小幅升级,最意外的是原生支持WP8.1版Cortana语音命令操作.要知道微软的聊天机器人“小冰”在微信上存在不到4天,就被微信全面封杀退出,现在微信又内测 ...
- 【C语言入门教程】1.1 基本程序结构
基本程序结构就是从上至下顺序执行的程序,C语言程序必须有且只有一个主函数,程序从主函数开始执行,直到主函数结束.下例是根据半径求圆形面积的程序源代码. #include <stdio.h> ...