lintcode :同构字符串
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.
Subscribe to see which companies asked this question
- public class Solution {
- public boolean isIsomorphic(String s, String t) {
- if(s==null || t ==null)
- return false;
- if(s.length()!=s.length())
- return false;
- if(s==null && t == null)
- return true;
- HashMap<Character,Character> map = new HashMap<Character,Character>();
- for(int i = 0;i< s.length();i++){
- char c1 = s.charAt(i);
- char c2 = t.charAt(i);
- Character c = getKey(map,c2);
- if(c !=null && c!=c1)
- return false;
- else if(map.containsKey(c1)){
- if(c2!=map.get(c1))
- return false;
- }else{
- map.put(c1,c2);
- }
- }
- return true;
- }
- public Character getKey(HashMap<Character,Character> map,Character target){
- for(Map.Entry<Character,Character> entry:map.entrySet()){
- if(entry.getValue().equals(target)){
- return entry.getKey();
- }
- }
- return null;
- }
- }
或者
- public class Solution {
- public boolean isIsomorphic(String s, String t) {
- if(s==null || t ==null)
- return false;
- if(s.length()!=s.length())
- return false;
- if(s==null && t == null)
- return true;
- HashMap<Character,Character> map = new HashMap<Character,Character>();
- for(int i = 0;i< s.length();i++){
- char c1 = s.charAt(i);
- char c2 = t.charAt(i);
- Character c = map.get(c1);
- // key c1 value c2
- if(map.containsKey(c1)){
- if(map.get(c1).equals(c2))
- continue;
- else
- return false;
- }else{
- if(!map.containsValue(c2))
- map.put(c1,c2);
- else
- return false;
- }
- }
- return true;
- }
- }
这里的字符串都是字母,前256个字符,将其映射到后256个字符
- public class Solution {
- public boolean isIsomorphic(String s, String t) {
- if(s==null || t ==null)
- return false;
- if(s.length()!=s.length())
- return false;
- if(s==null && t == null)
- return true;
- int[] m = new int[512];
- for (int i = 0; i < s.length(); i++) {
- if (m[s.charAt(i)] != m[t.charAt(i)+256])
- return false;
- m[s.charAt(i)] = m[t.charAt(i)+256] = i+1;
- }
- return true;
- }
- }
lintcode :同构字符串的更多相关文章
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205:同构字符串 Isomorphic Strings
题目: 给定两个字符串 s 和 *t*,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 *t* ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字 ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- Java实现 LeetCode 205 同构字符串
205. 同构字符串 给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序. ...
- [Swift]LeetCode205. 同构字符串 | Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【leetcode 简单】 第五十九题 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一 ...
- LeetCode OJ:Isomorphic Strings(同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Q205 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一 ...
- 205 Isomorphic Strings 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...
随机推荐
- Datawarehouse
- PB中掉用Run以后,等Run的程序关闭以后才会执行后边的语句
OleObject wsh integer li_rc CONSTANT integer MAXIMIZED = CONSTANT integer MINIMIZED = CONSTANT integ ...
- 详谈socket请求Web服务器过程
最开始我们需要明白一件事情,因为这是这篇文章的前提: HTTP协议只是一个应用层协议,它底层是通过TCP进行传输数据的.因此,浏览器访问Web服务器的过程必须先有“连接建立”的发生. 而有人或许会问: ...
- Using sql azure for Elmah
The MSDN docs contain the list of T-SQL that is either partially supported or not supported. For ex ...
- Codeforces Beta Round #69 (Div. 1 Only) C. Beavermuncher-0xFF 树上贪心
题目链接: http://codeforces.com/problemset/problem/77/C C. Beavermuncher-0xFF time limit per test:3 seco ...
- sentinel.conf配置
1.常用的配置 port 26379 # sentinel announce-ip <ip> # sentinel announce-port <port> dir /tmp ...
- Promises与Javascript异步编程
Promises与Javascript异步编程 转载:http://www.zawaliang.com/2013/08/399.html 在如今都追求用户体验的时代,Ajax应用真的是无所不在.加上这 ...
- NYOJ-102 次方求模 AC 分类: NYOJ 2014-02-06 18:53 184人阅读 评论(0) 收藏
地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=102 //a^b mod c=(a mod c)^b mod c很容易设计出一个基于二分的递归 ...
- [nowCoder] 二进制中1的个数
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. class Solution { public: int NumberOf1(int n) { ; while(n) ...
- AngularJs学习笔记--html compiler
原文再续,书接上回...依旧参考http://code.angularjs.org/1.0.2/docs/guide/compiler 一.总括 Angular的HTML compiler允许开发者自 ...