Java for LeetCode 205 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.
解题思路:
用两张图保存下两个方向的映射即可,JAVA实现如下:
public boolean isIsomorphic(String s, String t) {
HashMap<Character, Character> hm = new HashMap<Character, Character>();
HashMap<Character, Character> hm2 = new HashMap<Character, Character>();
for (int i = 0; i < s.length(); i++) {
if (hm.containsKey(s.charAt(i))
&& hm.get(s.charAt(i)) != t.charAt(i))
return false;
hm.put(s.charAt(i), t.charAt(i));
if (hm2.containsKey(t.charAt(i))
&& hm2.get(t.charAt(i)) != s.charAt(i))
return false;
hm2.put(t.charAt(i), s.charAt(i));
}
return true;
}
Java for LeetCode 205 Isomorphic Strings的更多相关文章
- [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 解题思路 - Java
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Java [Leetcode 205]Isomorphic Strings
题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- 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
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...
- (easy)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(哈希表)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Leetcode 205 Isomorphic Strings 字符串处理
判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...
随机推荐
- 高效的使用 Response.Redirect
介绍: 我正在评估一个 ASP.NET Web 项目应用.它有一些可扩展性问题.意味着当网站访问量增加的时候.系统将会变得缓慢.当我查看应用日志.我找到了大量的 ThreadAbortExceptio ...
- 密码学初级教程(一)基本概念及DES加密算法
密码技术在网络通信中广泛使用,本节是初步接触密码学技术的笔记. 第1章 加密-解密 破译 明文-密文 密钥 密码算法 对称密码-公钥密码(非对称密码) 单向散列函数-散列值 消息认证码 数字签名 伪随 ...
- Access应用日志<一>
今天在确认实习生不能帮忙搭建数据库后,自己根据业务需求尝试搭了一个小型access数据库. 主要目的:储存历史月度数据,避免每次从公司数据库下载数据的麻烦,节省数据拉取时间. 搭建了以acct id为 ...
- window8.1使用之快捷键
WIN键+? Win键——打开“开始”屏幕 Win+D——显示桌面 Win+E——打开计算机 Win+R——打开“运行”对话框 Win+L——锁定计算机 Win+M——最小化窗口 Win+方向键——窗 ...
- findByExample(Object exampleEntity)方法得到的List判断是否为空,不可用(lis != null)
用findByExample(Object exampleEntity)方法可以应用在用户登录上面,获得有登陆名和密码的user对象进行查询. 返回两者都符合的对象列表,为空则登陆失败. 错误的方法: ...
- C#调用java类、jar包方法
一.将已经编译后的java中Class文件进行打包:打包命令JAR 如:将某目录下的所有class文件夹全部进行打包处理: 使用的命令:jar cvf test.jar -C com/ . 其中tes ...
- oracle 彻底删除用户及表空间
1.删除表空间 可以先将其offline alter tablespace xxx offline; 将磁盘上的数据文件一同删除 drop tablespace xxx including conte ...
- Dijkstra(歪果仁的名字真是长。。。)
Dijkstra算法又称为单源最短路径,所谓单源是在一个有向图中,从一个顶点出发,求该顶点至所有可到达顶点的最短路径问题. 设G=(V,E)是一个有向图,V表示顶点,E表示边.它的每一条边 ...
- iOS开发——UI进阶篇(九)block的巧用
前面有提到通知.代理.kvo等方法来协助不同对象之间的消息通信,今天再介绍一下用block来解决这个问题 接着前面的例子 这里将功能在复述一遍 我把用block和通知放在一起比较,当然代理和kvo如何 ...
- js 判断鼠标进去方向
function fx(id){ var obj= document.getElementById(id); var fun=function(e){ var w=obj.offsetWidth; v ...