(String) 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.
public static boolean isIsomorphic(String s, String t) {
if(s==null && t==null) return true;
else if(s.length()!=t.length()) return false;
Map<Character,Character> hm=new HashMap<Character,Character>();
for(int i=0;i<s.length();i++) {
if(hm.containsKey(s.charAt(i))) {
if(hm.get(s.charAt(i))!=t.charAt(i))
return false;
}
else if(hm.containsValue(t.charAt(i)))
return false; //开始的时候忘了这个条件,要注意一下
else
hm.put(s.charAt(i), t.charAt(i));
}
return true;
}
(String) 205.Isomorphic Strings的更多相关文章
- 205. Isomorphic Strings - LeetCode
Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...
- [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
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
- *205. Isomorphic Strings (string & map idea)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- (easy)LeetCode 205.Isomorphic Strings (*)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- ViewHolder简洁写法
ViewHolder holder = null; if(convertView == null){ convertView = mInflater.i ...
- 如何获取EntityManager
1.在容器内部使用,使用@PersistenceContext 来注入.@PersistenceContextprivate EntityManager em;TAG================= ...
- codeforces 666A (DP)
题目链接:http://codeforces.com/problemset/problem/666/A 思路:dp[i][0]表示第a[i-1]~a[i]组成的字符串是否可行,dp[i][1]表示第a ...
- C# 计算时间差 用timespan函数
TimeSpan 结构 表示一个时间间隔. 命名空间:System 程序集:mscorlib(在 mscorlib.dll 中) 1.DateTime值类型代表了一个从公元0001年1月1日0点0分 ...
- echo、print、sprint、sprintf输出
echo() 函数 定义和用法 echo() 函数输出一个或多个字符串. 语法 echo(strings) 参数 描述 strings 必需.一个或多个要发送到输出的字符串. 提示和注释 注释:ech ...
- Oracle数据库初级学习 2
今天我们介绍Oracle数据库中剩余的查询方法,今天的查询方法会比昨天的更为复杂一些(PS:我也是个初学者,请见谅..). 一.分组函数 分组函数是为了区分同一个表中的不同数据而建立,其关键字为GRO ...
- mysql控制台操作
显示表结构 : show create table table_name 复制表:insert into table_name1 select * from table_name2
- Gvim+Emmet.vim 那些事。
转自:http://www.jianshu.com/p/67fa1e2114c5 背景 HTML和CSS的写法相对固定,而且重复的工作特别多,特别是尖括号实在是很烦.使用Emmet至少能帮你节省一半的 ...
- python数据结构与算法——字典树
class TrieTree(): def __init__(self): self.root = {} def addNode(self,str): # 树中每个结点(除根节点),包含到该结点的单词 ...
- Linux文件操作 笔记
fstat stat lstat 原型 #include <unistd.h> #include <sys/stat.h> #include <sys/types.h&g ...