LeetCode_205. Isomorphic Strings
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.
Example 1:
Input: s ="egg",
t ="add"
Output: true
Example 2:
Input: s ="foo",
t ="bar"
Output: false
Example 3:
Input: s ="paper",
t ="title"
Output: true
Note:
You may assume both s and t have the same length.
package leetcode.easy; import java.util.HashMap; public class IsomorphicStrings {
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) {
return false;
}
HashMap<Character, Character> map = new HashMap<Character, Character>();
for (int i = 0; i < s.length(); i++) {
char a = s.charAt(i);
char b = t.charAt(i);
if (map.containsKey(a)) {
if (map.get(a).equals(b)) {
continue;
} else {
return false;
}
} else {
if (!map.containsValue(b)) {
map.put(a, b);
} else {
return false;
}
}
}
return true;
} @org.junit.Test
public void test() {
String s1 = "egg";
String t1 = "add";
String s2 = "foo";
String t2 = "bar";
String s3 = "paper";
String t3 = "title";
System.out.println(isIsomorphic(s1, t1));
System.out.println(isIsomorphic(s2, t2));
System.out.println(isIsomorphic(s3, t3));
}
}
LeetCode_205. 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
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
随机推荐
- dedecms自定义表单提交获取时间跟ip地址
相信大家在用织梦做网站的时候都用过自定义表单做留言,但是如何查看客户什么时间填写的表单,和客户的IP地址呢? 我在网上找了很多JS文件,但太繁琐了,后来我注意到一个细节,每次我登陆后台,织梦系统都会记 ...
- LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)
链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...
- mysql模糊查询多个字段
SELECT * FROM nst_t_conferencehis WHERE ConferName REGEXP '军工|军资';
- 2019.11.29 Mysql的数据操作
为名为name的表增加数据(插入所有字段) insert into name values(1,‘张三’,‘男’,20); 为名为name的表增加数据(插入部分字段) insert into name ...
- 自行撰写Grasshopper电池
Grasshopper目前作为参数化设计是非常常用的工具,但是人们会经常碰到它提供的电池不能满足自己设计方案需求的情况,所以就需要自己创作电池,而最简单的一种方法就是自己写. 工具: Visual S ...
- [luogu 3773][CTSC 2017]吉夫特
传送门 Solution 输入一个长度为n的数列,求有多少个长度大等于2的不上升子序列满足: \[\prod_{i=2}^{k} C(a_{b_{i-1}},a_{b_i}) mod\ 2 > ...
- c和s标签
<s:if test="#request.iflag=='fj'"> <a title="复检录入" href="javascrip ...
- 6、vueJs基础知识06
vue动画 transition 之前1.0版本是以 属性的形式展示的 <p transition="fade"></p> .fade-transition ...
- DB2通过某列分组来去重
DB2通过某列分组来去重,可防止distinct对大字段的去重报错. row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分 ...
- 用sublime3编写运行16位汇编程序_详细教程
最近需要学8086汇编,课堂教学竟然是PPT看代码,然而不运行程序是没法学编程的.网上的教程有很多坑点,摸索出了正确的步骤. 1.安装sublime3.安装MASM32.64位系统安装DOSBOX(因 ...