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.

Accepted solution:

create an array: map all the character and count the value as the index of each characters in string. start from 1

e.g.: egg & dda & add

a['e'] = 1 a['g'] = 2 -> 3 ; a['d'] = 1 -> 2  a['a'] 3 ; a['a'] = 1   a['d'] 2 -> 3

class Solution {
public boolean isIsomorphic(String s, String t) {
//letter , index of string
//e.g. egg : e: 1 g:2 g:3
if(s.length()!=t.length()) return false;
int[] a = new int[256];
int[] b = new int[256];
for(int i = 0; i<s.length(); i++){
if(a[s.charAt(i)] != b[t.charAt(i)]) return false;
//update the character
a[s.charAt(i)] = i+1; // why plus 1 for the case aa & ab
b[t.charAt(i)] = i+1;
}
return true;
}
}

Good idea make the code clean and easy. Think before coding.

*205. Isomorphic Strings (string & map idea)的更多相关文章

  1. 205. Isomorphic Strings - LeetCode

    Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...

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

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

  3. 【刷题-LeetCode】205. Isomorphic Strings

    Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...

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

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

  5. (String) 205.Isomorphic Strings

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

  6. 205. Isomorphic Strings (Map)

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

  7. LeetCode 205 Isomorphic Strings

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

  8. Java for LeetCode 205 Isomorphic Strings

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

  9. 205 Isomorphic Strings

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

随机推荐

  1. c++从txt中读取数据,数据并不是一行路径(实用)

    #include <iostream>#include <fstream>#include <string> using namespace std; //输出空行 ...

  2. 苏D_8M150

    20161226 麦德龙 西边 弄堂 前车 转弯 刹车,我 刹车,下雨路滑,滑到.没撞到前车.车型号没看...只记了车牌... 右手撑了一下,肩膀估计是撑伤了,举起来 比较疼... 不知 该如何处理, ...

  3. js学习笔记 -- 函数

    js函数有类似javaMethod用法 Math.max.apply( Math.max.call( Array map,reduce,filter,sort , , , , , , , , ]; v ...

  4. MySQL三大范式和反范式

    1. 第一范式确保数据表中每列(字段)的原子性.如果数据表中每个字段都是不可再分的最小数据单元,则满足第一范式.例如:user用户表,包含字段id,username,password 2. 第二范式在 ...

  5. python_文件 处理

    一.字符编码 内存固定使用unicode编码 数据最先产生于内存中,是unicode格式,要想传输需要转成bytes格式 # unicode -------> enconde( u t f - ...

  6. 2019.03.21 读书笔记 ==与Equals

    首先得出一个结论:==是比较变量内存的数据,Equals是值比较.但是他们都能被重写,所以object又增加了一个RefrenceEquals不可被重写,只比较数据: [ReliabilityCont ...

  7. iTween 不能两个游戏对象同时用一个Hashtable

    两个游戏对象,点击其中一个,两个对象一起旋转,再点一下,两个都旋转到原来角度. 如图:两个游戏对象不能用一个Hashtable,会出错,达不到两个一起转的效果. 每个对象要有自己的Hashtable来 ...

  8. DRF-->1 序列化组件的使用和接口设计---get

    定义序列化器(本质就是一个类),一般包括模型类的字段,有自己的字段类型规则.实现了序列化器后,就可以创建序列化对象以及查询集进行序列化操作,通过序列化对象.data来获取数据(不用自己构造字典,再返回 ...

  9. Mongodb installation & userguide

    1.Mongodb Installation in Ubuntu (1) Download from: https://www.mongodb.org/downloads File: mongodb- ...

  10. HDU 4612——Warm up——————【边双连通分量、树的直径】

    Warm up Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Stat ...