*205. Isomorphic Strings (string & map idea)
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.
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)的更多相关文章
- 205. Isomorphic Strings - LeetCode
Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...
- [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
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
- LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...
- (String) 205.Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 205. Isomorphic Strings (Map)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- springMvc使用restful风格
转载:https://blog.csdn.net/weide_java/article/details/53793769 1,REST架构师一个抽象的概念,目前主要是基于HTTP协议实现,其目的是为了 ...
- java泛型&bean copy list
参考:https://www.oracle.com/technetwork/cn/articles/java/juneau-generics-2255374-zhs.html E:元素K:键N:数字T ...
- WindowsMTU修改
MTU是英文Maximum Transmission Unit的缩写,意为"最大传输单位".也就是通过TCP/IP协议所传输的数据包最大有多少字节,对于网速有极大的影响, MTU并 ...
- java——变量
1.静态变量: 随着类的加载而生成并初始化 随着类的消失而消失 2.成员变量: 随对象的加载而生成并初始化 随对象被回收而消失 3.局部变量: 作用范围由{}决定 随方法调用而创建 随方法的执行完毕而 ...
- 数据库版本管理工具flyway
引入flyway_core jar包 java 代码实现 public class FlywayMigration { @Resource private DataSource dataSource ...
- android通过Jni加载so库遇到UnsatisfiedLinkError问题!!!
错误信息: java.lang.UnsatisfiedLinkError: hsl.p2pipcam.nativecaller.NativeCaller at hsl.p2pipcam.manager ...
- PlayMaker Play Sound 和 Audio Play
这两个 Action 都可以播放声音 *Play Sound:只要把声音拖进去就可以: *Audio Play:要求游戏对象要有Audio Source组件.
- rails4 ckeditor 的部署以及 中文化
首先ckeditor 要基于paperclip 之后paperclip 需要你在linux 下安装 ImageMagick 具体安装可参考http://my.eoe.cn/guanmac/arc ...
- c语言中的隐式函数声明(转)
本文转自:http://www.jb51.net/article/78212.htm 在c语言里面开来还是要学习c++的编程习惯,使用函数之前一定要声明.不然,即使编译能通过,运行时也可能会出一些莫名 ...
- redis4集群
三台服务器:先设置hosts 10.0.0.231 node1 10.0.0.232 node2 10.0.0.233 node3 端口分配: node1: node1: node2: node2: ...