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 ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...
随机推荐
- 关于UIView需要看的一些官方文档
View Controller PG(Programming Guide) 看过一遍 View PG 正在看 Drawing and Printing PG Quartz 2D PG 更高级的cus ...
- 62.在cdc文件上某些例化模块看不到的原因
比如在顶层文件中,例化了几个模块,综合后打开cdc文件,会在structure/net中少几个例化模块,即看不到,但在顶层文件中还是存在的,只是ISE软件综合的问题而已,原因是在顶层或子模块中,有些应 ...
- Hibernate从入门到精通(十)多对多单向关联映射
上一篇文章Hibernate从入门到精通(九)一对多双向关联映射中我们讲解了一下关于一对多关联映射的相关内容,这次我们继续多对多单向关联映射. 多对多单向关联映射 在讲解多对多单向关联映射之前,首先看 ...
- chmod修改文件权限的命令
语法: chmod [options] mode files options: -c,--changes只输出被改变文件的信息-f,--silent,--quiet当chmod不能改变文件模式时,不通 ...
- Windows完成端口网络模型
GetQueuedCompletionStatus 比如此时端口上完成的是什么操作,数据是什么等,还有,系统如何做到自动填充上述的结构的,也就是说,系统怎么知道在Overlap->OpCode ...
- why does angular js rock
angularjs 入门教程 http://angular-tips.com/blog/2013/08/why-does-angular-dot-js-rock/ Practive the previ ...
- shell基本语法备忘
1.第一行要写明shell种类 #!/bin/bash 2.打印输出 #!/bin/bashecho "Hello World !~" 3.变量定义 变量=前后不能有空格, ...
- Python python 基本语法
程序1 def buildConnectionString(params): """Build a connection string from a dictionary ...
- How to find and fix Bash Shell-shock vulnerability CVE-2014-6271 in unix like system
type command - env x='() { :;}; echo vulnerable' bash -c 'echo hello' in your terminal. if your sy ...
- JVM 崩溃 Failed to write core dump解决办法 WINDOWS
JVM 崩溃 Failed to write core dump解决办法 WINDOWS MIT key words: JVM,崩溃,windows,Failed,core dump,虚拟内存 最近从 ...