[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.
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.
思路1(用两个map)
1. scan char a from S and char b from T in the same time
2. use two int array to mimic hash table: mapAB, mapBA
3. if mapAB[a] == 0 , means I didn't mapping it, assign current b to mapAB[a]
otherwise, means I did mapping it, check b == mapAB[a] ?
4. do the same operation for mapBA
5. why checking two maps at the same time? Coz S: egg -> T: aaa return true
then we still check T: aaa -> S: egg
代码
class Solution {
public boolean isIsomorphic(String s, String t) {
if(s.length()!=t.length()) return false;
// uses the arrays to mimic two hash table
int [] mapAB = new int[256];//ASCII characters
int [] mapBA = new int[256];
for(int i = 0; i< s.length();i++){
char a = s.charAt(i);
char b = t.charAt(i);
// mapAB[a]!=0 means I already mapping it
if(mapAB[a]!=0){
if(mapAB[a]!=b) return false;
}
// mapAB[a]==0 means I haven't mapping it
else{
mapAB[a]= b;
} // why checking two map? coz S:egg T: aaa would return true if only checking mapAB
if(mapBA[b]!=0){
if(mapBA[b]!=a) return false;
}else{
mapBA[b] = a;
}
}
return true;
}
}
思路2(只用一个map)
1. scan either S or T(assuming they have same length)
2. store the idx of current char in both strings.
if previously stored idx are different from current idx, return false
举例:
S: egg T: aaa
m[e] = 1 m[a+256] = 1
m[g] = 2 occurs that previous m[a+256] = 1 return false
代码
class Solution {
public boolean isIsomorphic(String s, String t) {
if(s.length() != t.length()) return false;
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;
}
} /* Why not m[s.charAt(i)] = m[t.charAt(i)+256] = i ?
coz 0 is the default value, we should not use it. otherwise we cannot distinguish between
the default maker and the the marker we made.
S: aa T: ab
i= 0: m[a] = 0 m[a+256] = 0
but m[b+256] is defaulted 0 */
followup1:
如果输入K个string,判断其中至少两个是Isomorphic Strings, 返回boolean
思路
1. 将所有的given string都转成同一种pattern
ex. foo -> abb
ex. gjk -> abc
ex. pkk -> abb
2. 用一个hashmap来存 transfered word 和其出现的频率。
key : value(frequency)
ex. foo -> abb : 1
ex. gjk -> abc : 1
ex. pkk -> abb : 1+1 return true
代码
public boolean findIsomorphic(String[] input) {
// key: transWord, value: its corresponding frequency
Map<String, Integer> map = new HashMap<>();
for (String s : input) {
// transfer each String into same pattern
String transWord = transfer(s);
if (!map.containsKey(transWord)) {
map.put(transWord, 1);
}
// such transWord pattern already in String[]
else {
return true;
}
}
return false;
} /* pattern: every word start with 'a'
when comes a new letter, map it to cur char,
and increase the value of cur cha
*/
private String transfer(String word) {
Map<Character, Character> map = new HashMap<>();
StringBuilder sb = new StringBuilder();
char cur = 'a';
for (char letter : word.toCharArray()) {
if (!map.containsKey(letter)) {
map.put(letter, cur);
cur++;
}
sb.append(map.get(letter));
}
return sb.toString();
}
followup2:
如果输入K个string, 判断其中任意两两是Isomorphic Strings,返回boolean
即给定K个string都能化成同一种等值的pattern
[leetcode]205. Isomorphic Strings 同构字符串的更多相关文章
- [leetcode]205. Isomorphic Strings同构字符串
哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查 ...
- 205 Isomorphic Strings 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...
- [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(同构的字符串)(string、vector、map)(*)
翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...
- 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 ...
- 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 解题思路 - Java
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- PythonStudy——字符串扩展方法 String extension method
')) ')) print('***000123123***'.lstrip('*')) print('***000123123***'.rstrip('*')) print('华丽分割线'.cent ...
- bootstrap modal 移除数据
有时业务需求,在弹出模态框时需要动态的展示,但是不做处理时,每次弹出的都是第一次的数据,所以需要监听模态框关闭,每次关闭需要先清除里面的数据. //监听弹页关闭 $("#myModal&qu ...
- 在xcode 上调试c程序
打开xcode 选择 Create a new Xcode project 选择Command Line Tool 给你的项目起个名,选择c语言 点击next 选择存储位置,就会制动生成一个项目,在项 ...
- 根据CPU核心数确定线程池并发线程数
一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...
- centos查看系统版本信息
1.查看版本文件名称 ll /etc/*centos* 2.显示系统版本号 cat /etc/centos-release
- Unix/Linux进程间通信
一,Linux下进程间通信的几种主要手段简介: 1,管道(Pipe)及有名管道(named pipe) 管道可用于具有亲缘关系进程间的通信 有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功 ...
- 【python】dist-packages和site-packages的区别
一.dist-packages和site-packages的区别 sudo apt-get install 安装的package存放在/usr/lib/python2./dist-packages目录 ...
- logback不输出日志消息,且SLF4J绑定源错误
我之前的项目已经成功使用过logback作为日志输出,但是今天新项目在使用的时候,不输出日志信息. 最后终于找到问题所在,并成功解决.解决步骤如下: 第一步:检查pom.xml 按照以往惯例,我先检查 ...
- android ButterKnife 点击事件没反应的解决方案
可能只添加了 implementation 'com.jakewharton:butterknife:8.8.1'而没有添加下面这行 annotationProcessor 'com.jakewhar ...
- Linux常用命令大全(分类)
首先按ESC键回到命令模式: vi保存文件有不同的选项,对应于不同的命令,你可以从下面的命令中选择一个需要的::w 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi:w! ...