*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 ...
随机推荐
- 上传文件插件dropzone的实例
html: <div class="field"> <div id="file" class="dropzone"> ...
- Centos 7.4 配置Tomcat管理员用户
1,进入Tomcat路径下的conf文件夹 ,编辑tomcat-users.xml文件 2,在<tomcat-users>标签中增加user标签,用户名密码随便填写,roles可根据权限需 ...
- HIVE分析函数
ROWS BETWEEN含义,也叫做WINDOW子句: PRECEDING:往前 FOLLOWING:往后 CURRENT ROW:当前行 UNBOUNDED:起点,UNBOUNDED PRECEDI ...
- 用sphinx-doc优雅的写文档
Sphinx 是一个工具,它使得创建一个智能而美丽的文档变得简单.作者Georg Brandl,基于BSD许可证. 起初为写 Python 文档而诞生的 Sphinx,支持为各种语言生成软件开发文档. ...
- mc02_配置本地git仓库并上传到github
注册github账号 仔细阅读使用说明便可,这里提一下如何删除一个repository. 点击要删除的repository,打开后点击Settings 然后滚动到页面最下方,点击最后一个按钮 在弹出框 ...
- 解决Yii2 添加css后页面刷新也无反应的情况
'assetManager' => [ // uncomment the following line if you want to auto update your assets (unix ...
- 【ACM】无聊的小明
无聊的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 这天小明十分无聊,没有事做,但不甘于无聊的小明聪明的想到一个解决无聊的办法,因为他突然对数的正 ...
- PS使模糊图片变清晰
操作步骤 \(文件\)
- OpenStack概念
OpenStack is a global collaboration ofdevelopers and cloud computing technologists producing the ubi ...
- SuperSpider(简书爬虫JAVA版)
* 建站数据SuperSpider(简书)* 本项目目的:* 为练习web开发提供相关的数据:* 主要数据包括:* 简书热门专题模块信息.对应模块下的热门文章.* 文章的详细信息.作者信息.* 评论区 ...