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 ...
随机推荐
- Nginx + FastCGI 程序(C/C++) 搭建高性能web service的Demo及部署发布
FastCGI编程包括四部分:初始化编码.接收请求循环.响应内容.响应结束循环. FCGX_Request request; FCGX_Init(); ); FCGX_InitRequest(& ...
- 存储过程中的when others then 和 raise
EXCEPTION when others then rollback; dbms_output.put_line('code:' || sqlcode); dbms_output.put_line( ...
- [Js/Jquery]jquery插件开发
摘要 上篇文章简单学习了js自调用方法.今天就趁热打铁,学一学怎么编写一个jquery插件. JQuery 参考地址:http://www.cnblogs.com/playerlife/archive ...
- Kafka入门经典教程
本帖最后由 desehawk 于 2015-5-3 00:45 编辑问题导读 1.Kafka独特设计在什么地方?2.Kafka如何搭建及创建topic.发送消息.消费消息?3.如何书写Kafka程 ...
- Linux服务器管理: 日志管理(一)
1.日志管理介绍: a.日志服务:在CentOS6.x中日志服务以及由rsyslogd取代了原有的syslogd服务.rsyslogd日志服务更加先进,功能更多.但是不论该服务的使用,还是日子文件的格 ...
- 《C++编程规范》
1.使用编译器的最高警告级别,成功的构建应该是无声无息的(没有警告的). 如果确定是无害警告,且是无法修改的第三方头文件引起的,可以用自己的头文件包装起来,并有选择性的关闭警告,然后项目中使用该头文件 ...
- 给一系列的div中的第一个添加class
$(".lb:first").addClass("active")
- phpcms2008 常用数组,变量整理
代码:$_userid 用户id $_username 用户名 $_areaid 地区id $_groupid 用户组id $_modelid $_amount 用户资金 $_point 用户点数 $ ...
- C++ GET UTF-8网页编码转换
string UTF8ToGBK(const std::string& strUTF8) //GBKתUTF-8 { int le ...
- iOS开发——UI进阶篇(九)block的巧用
前面有提到通知.代理.kvo等方法来协助不同对象之间的消息通信,今天再介绍一下用block来解决这个问题 接着前面的例子 这里将功能在复述一遍 我把用block和通知放在一起比较,当然代理和kvo如何 ...