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 and 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 同构字符串的更多相关文章

  1. [leetcode]205. Isomorphic Strings同构字符串

    哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查 ...

  2. 205 Isomorphic Strings 同构字符串

    给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...

  3. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  4. LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

    翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...

  5. LeetCode 205. Isomorphic Strings (同构字符串)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  6. Leetcode 205 Isomorphic Strings 字符串处理

    判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...

  7. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

  8. [LeetCode] 205. Isomorphic Strings 解题思路 - Java

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. Java for LeetCode 205 Isomorphic Strings

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

随机推荐

  1. 【java多线程】队列系统之DelayQueue源码

    一.延迟队列 延迟队列,底层依赖了优先级队列PriorityBlockingQueue 二.延迟队列案例 (1)延迟队列的任务 public class DelayTask implements De ...

  2. 使用openresty && minio && thumbor 构建稳定高效的图片服务器

    备注: minio 是一个开源的s3 协议兼容的分布式存储,openresty nginx+lua 高性能可扩展的nginx 衍生版,thumbor 基于python 的图片处理服务器,支持图片的裁剪 ...

  3. 数学:确定性的丧失 (M·克莱因 著)

    第一章 数学真理的起源 (已看) 第二章 数学真理的繁荣 (已看) 第三章 科学的数学化 (已看) 第四章 第一场灾难:真理的丧失 (已看) 第五章 一门逻辑科学不合逻辑的发展 (已看) 第六章 分析 ...

  4. django基础 -- 10.form , ModelForm ,modelformset

    一.生成页面可用的 HTML标签 1.form 所有内置字段 Field required=True, 是否允许为空 widget=None, HTML插件 label=None, 用于生成Label ...

  5. Spring生态研习【四】:Springboot+mybatis(探坑记)

    这里主要是介绍在springboot里面通过xml的方式进行配置,因为xml的配置相对后台复杂的系统来说,能够使得系统的配置和逻辑实现分离,避免配置和代码逻辑过度耦合,xml的配置模式能够最大限度的实 ...

  6. NodeJs中类定义及类使用

    1.首先定义类Point,文件名为point.class.js: // 定义类 class Point { //构造函数 constructor(x, y) { this.x = x;//类中变量 t ...

  7. Scrapy学习篇(十一)之设置随机User-Agent

    大多数情况下,网站都会根据我们的请求头信息来区分你是不是一个爬虫程序,如果一旦识别出这是一个爬虫程序,很容易就会拒绝我们的请求,因此我们需要给我们的爬虫手动添加请求头信息,来模拟浏览器的行为,但是当我 ...

  8. Unity Shader Graph(一)初次尝试

    软件环境 Unity Version: 2018.1.2f1 边缘发光材质效果 创建工程 打开Unity并创建一个新工程 安装依赖项 Window -> Package Manager打开包管理 ...

  9. 涂抹mysql笔记-数据导出导入

    数据导出导入<>利用CSV存储引擎加载数据:CSV存储引擎基于CSV格式文件存储数据,CSV格式是纯文本格式的文件,以逗号分隔取值.CSV引擎表的所有列值不能为空.Excel可以直接打开有 ...

  10. SHFileOperation 解决double-null terminated

    void rubyTools::funStrToWstr(string str, wstring& strw) { const char* pData = str.c_str(); int l ...