LeetCode 205. Isomorphic Strings (同构字符串)
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.
For example,
Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.
Note:
You may assume both s and t have the same length.
题目标签:Hash Table
题目给了我们两个string, 让我们判断它们是不是isomorphic。
只有当两个string的 char 是 1对1 map的时候,才是isomorphic,那么来看一下两个情况,它们不是 1对1 map:
1. bar foo
map:
b -> f
a -> o
r -> o
这种情况就是 左边两个chars map 到了 同一个 右边的 char;
2. bb fa
map:
b -> f
b -> a
这种情况就是 右边两个chars map 到了 同一个 左边的char;
所以只要遇到这两种情况,返回false,剩下的就是true。
Java Solution:
Runtime beats 57.23%
完成日期:05/26/2017
关键词:HashMap
关键点:利用HashMap来记录1对1map
class Solution
{
public boolean isIsomorphic(String s, String t)
{
HashMap<Character, Character> map = new HashMap<>(); for(int i=0; i<s.length(); i++)
{
char sChar = s.charAt(i);
char tChar = t.charAt(i); if(map.containsKey(sChar))
{
if(!map.get(sChar).equals(tChar)) // case 1: two different right chars map to one left char
return false;
}
else
{
if(map.containsValue(tChar)) // case 2: two different left chars map to one right char
return false; map.put(sChar, tChar);
} } return true;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
LeetCode 205. Isomorphic Strings (同构字符串)的更多相关文章
- [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同构字符串
哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查 ...
- 205 Isomorphic Strings 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...
- Leetcode 205 Isomorphic Strings 字符串处理
判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- [LeetCode] 205. Isomorphic Strings 解题思路 - Java
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- 更自然的解决字典数组插入nil而导致crash
最近在优化项目虽说小优化一直在持续,大版本的优化也进行了两个版本了但是bug列表依旧血淋淋的摆在那里.有的看一眼也能找到问题所在但是有的就是想破头也不知道问题在哪里,毕竟整个项目经过了N个人的手代码风 ...
- [Android FrameWork 6.0源码学习] View的重绘过程之Draw
View绘制的三部曲,测量,布局,绘画现在我们分析绘画部分测量和布局 在前两篇文章中已经分析过了.不了解的可以去我的博客里找一下 下面进入正题,开始分析调用以及函数原理 private void pe ...
- 浅谈SQL优化入门:2、等值连接和EXPLAIN(MySQL)
1.等值连接:显性连接和隐性连接 在<MySQL必知必会>中对于等值连接有提到两种方式,第一种是直接在WHERE子句中规定如何关联即可,那么第二种则是使用INNER JOIN关键字.如下例 ...
- birt 报表设计总结
1, 通过sql查询出来的数据,当某个字段没有值时,我们期望显示别的东东 在表格单元格或者网格中选中这个值, 在属性编辑器-->映射--> 在映射表中添加映射条件 映射中当使用 等于 时, ...
- Linux下Birt、JTreeChart中文乱码问题解决办法
Linux下JTreeChart,Birt等报表工具显示中文乱码解决 1) 现象: 在Windows上生成的报表图片展示正常,但是在Linux上显示(怪异的小方块“口”): 2)问题定位: 这是lin ...
- vue 2 使用Bus.js进行兄弟(非父子)组件通信 简单案例
vue2中废弃了$dispatch和$broadcast广播和分发事件的方法.父子组件中可以用props和$emit().如何实现非父子组件间的通信,可以通过实例一个vue实例Bus作为媒介,要相互通 ...
- CSS的常用属性
刚开始学习前段的我,还处于初级阶段,一些东西还是会有搞不明白的时候,还是要大家多多理解.今说就一些关于CSS的常用属性吧! 一.CSS常用选择器 CSS选择器应该说是一个非常重要的工具吧,选择器用得好 ...
- 腾讯x5Webview取代原生android Webview
一.官方地址: https://x5.tencent.com/tbs/ 二.不需要申请开发者,QQ直接登录,下载即可集成到项目中. 三.与原生的webview对比优势 1) 速度快:相比系统webvi ...
- 自定义工作流活动报错:您无法登陆系统。原因可能是您的用户记录或您所属的业务部门在Microsoft Dynamics 365中已被禁用。
本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复265或者20170926可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me ...
- JAVA设计模式总结之23种设计模式
上一篇总结了设计模式的六大原则<JAVA设计模式总结之六大设计原则>,这一篇,正式进入到介绍23种设计模式的归纳总结. 一.什么是设计模式 ...