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. CF628D Magic Numbers (数据大+数位dp)求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数

    题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).a,b<10^2000,m≤2000,0≤d≤9 a,b< ...

  2. A. Cinema Line

    A. Cinema Line time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  3. Ubuntu14.10:Install Apache,PHP,Mysql以及扩展库

    step 1: Apache sudo apt-get install apache2 After have apache2 installed, go to localhost by browser ...

  4. js Form表单转json格式,及后台接收(多种方法)

    转载:https://blog.csdn.net/qq_40138785/article/details/81533015 一.serialize()方法格式:var data = $("# ...

  5. 转——jdbcType与javaType的对应关系

    ------------------------------------------------ 原文:https://blog.csdn.net/haofeng82/article/details/ ...

  6. OS---外存分配方式

    1.概述 1.1 在为文件分配外存空间时,所考虑的主要问题:如何有效利用外存空间?如何提高对文件的访问速度? 1.2 常用的外存分配方法:连续分配.链接分配.索引分配(在一个系统中,仅采用一种分配方式 ...

  7. android Binder机制(一)架构设计

    Binder 架构设计 Binder 被设计出来是解决 Android IPC(进程间通信) 问题的.Binder 将两个进程间交互的理解为 Client 向 Server 进行通信. 如下:bind ...

  8. Android NDK开发 Jni中Debug(三)

    下载LLDB 配置Android Native - Debugger 调式结果如下 #include <jni.h> #include <string.h> #include& ...

  9. python 合并重叠数据

  10. GBDT,FM,FFM推导

    GBDT推导: https://xgboost.readthedocs.io/en/latest/tutorials/model.html FM,FFM推导: https://tech.meituan ...