*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 ...
随机推荐
- poj 3977 子集
题目 题意:在一个集合中找到一个非空子集使得这个子集元素和的绝对值尽量小,和绝对值相同时保证元素个数尽量小 分析:1.二分枚举的思想,先分成两个集合: 2.枚举其中一个集合中所有的子集并且存到数组中, ...
- B. Sereja and Suffixes
B. Sereja and Suffixes time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CHUCK手把手带你搞定OPENSTACK
一.OpenStack初探 1.1 OpenStack简介 OpenStack是一整套开源软件项目的综合,它允许企业或服务提供者建立.运行自己的云计算和存储设施.Rackspace与NASA是最初重要 ...
- struts1学习
转载:https://blog.csdn.net/toyouheart/article/details/4509466
- python中bytes类型转换为str类型
使用的原因:基于URL解析报文的时候,要使用str类型,但是提供的确实bytes类型,报错: TypeError: must be str, not bytes 所以就把bytes类型转换为str类型 ...
- jupyter notebook自动补全功能实现
Jupyter notebook使用默认的自动补全是关掉的.要打开自动补全,需修改默认配置. 命令行中输入:ipython profile create 以上命令会在~/.ipython/profil ...
- python django 基本测试 及调试
#########20181110from django.db import modelsfrom blog.models import Article, Author, TagAuthor.obje ...
- oracle 12.1.0.2的mgmt 导致的ORA-01017 bug
两节点12c RAC,在两节点上export ORACLE_SID再sqlplus / as sysdba都正常登录,然而Commvault通过service_name方式(sqlplus sys/p ...
- oracle count(*) 和count(列)性能
一直以为oracle中count(列)比count(*) 快,这篇文件解释了一下: http://blog.csdn.net/szstephenzhou/article/details/8446481
- 不要滥用SharedPreference
SharedPreference是Android上一种非常易用的轻量级存储方式,由于其API及其友好,得到了很多很多开发者的青睐.但是,SharedPreference并不是万能的,如果把它用在不合适 ...