*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 ...
随机推荐
- eval()解析json以及js中js数组、对象与json之间的转换
http://www.cnblogs.com/myjavawork/articles/1979279.html https://www.cnblogs.com/coder-economy/p/6203 ...
- Oracle 树形SQL语句,SYS_CONNECT_BY_PATH 函数
转一个SYS_CONNECT_BY_PATH 函数的例子.推断原表应该是这样: Child Parent ------------------------ ...
- RequireJS -Javascript模块化(二、模块依赖)
上一篇文章中简单介绍了RequireJs的写法和使用,这节试着写下依赖关系 需求描述:我们经常写自己的js,在元素选择器这方面,我们可能会用jquery的$("#id")id选择器 ...
- oracle merge into函数中插入clob字段
当使用Merge into 函数向ORACLE数据库中插入或更新数据时,报错“ORA-01461: 仅可以为插入 LONG 列的 LONG 值赋值”,使用如下方法可以把String转换为clob. 需 ...
- Java 实践
/** *宠物就是一个标准,包含多类宠物 *定义宠物标准接口Pet *定义Cat和Dog两个Pet接口的子类 *使用链表结构动态存储宠物信息 *定义商店类(工厂类),负责宠物的上架(链表添加).下架( ...
- equals和等号的区别
如果是基本类型,等号比较的是数值.如果是引用类型,等号比较的是地址.而equals如果没有重写的话默认比较的是地址,可以重写equals来自定义比较两个对象的逻辑.
- Cookie和Session入门(一)
目录一)背景介绍二)Cookie机制三)Session机制四)两者比较五)参考资料链接一)背景介绍Cookie与Session是常用的会话跟踪技术.1.Cookie通过在客户端记录信息确定用户身份,S ...
- 多线程编程_控制并发线程数的Semaphore
简介 Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源.很多年以来,我都觉得从字面上很难理解Semaphore所表达的含义,只能把它比作是 ...
- RTT设备与驱动之SPI
SPI全双工设备的操作分为主设备和从设备(可以多个,多线程下从设备访问主设备要先获得总线控制权) rt_device_t rt_device_find(const char* name);查找设备 s ...
- STM32 从M3到M4
一 考虑STM32不同系列移植的外设资源情况: STM32微控制器应用的移植和兼容性指南AN3364 二 M4的DSP/FPU的使用方法https://blog.csdn.net/electrocra ...