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 ...
随机推荐
- Java:类类型变量
在java中有一种被称为类类型的变量,它不同于基本类型变量存储值的方式.不管是基本变量还是类类型变量,都实现为一个内存位置.但是,由于基本变量所需的内存数量是相同的,所以系统可以给它设置一个固定的空间 ...
- eclipse中svn插件在线安装方式
SVN插件地址:http://subclipse.tigris.org/update_1.8.x 第一步:eclipse>Help菜单>Install New Software- 第二步: ...
- 巧用 BootStrap --- 栅格系统(布局)轻松搞定网页响应式布局!
摘要:Bootstrap 为我们提供了一套响应式.移动设备优先的流式栅格系统,合理的使用栅格系统将会使得网站页面布局变得更加简单,在设置了媒体查询之后,响应式网站也无需再单独写了.接下来我以Boots ...
- 搭建连接MySql的三层架构的ASP.NetCore2.0的WebApi
里我们用三层架构搭建一个连接MySql的ASP.netCore模板的WebApi项目 首先添加WebApi项目(ASP.NetCore版本) 右键解决方案>新建项目> 选择Web>A ...
- JSP知识点大纲图
这是我整理出来的JSP知识点大纲图,具体的内容都可以在我的博文中找到-.
- String类的一些转换功能(6)
1:把字符串转换成字节数组 getBytes() 如: String s = "你好啊!" //编码 byte [] arr = s.getBytes();//这里默认编码格式是g ...
- 再起航,我的学习笔记之JavaScript设计模式25(迭代器模式)
迭代器模式 概念介绍 迭代器模式(Iterator): 在不暴露对象内部结构的同时,可以顺序地访问聚合对象内部的元素. 迭代器 程序中的循环是一种利器,循环语句也使我们程序开发更简洁高效,但是有时一遍 ...
- 再起航,我的学习笔记之JavaScript设计模式26(解释器模式)
解释器模式 概念介绍 解释器模式(Interpreter):给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 获取元素在页面中的路径 我们都知道获取一个 ...
- 关于数据库中datareader的用法
1.C#中提供的DataReader可以从数据库中每次提取一条数据. using System; using System.Collections.Generic; using System.Comp ...
- Java中的对象和引用
<Java编程思想>中有一段关于对象的说法: "按照通俗的说法,每个对象都是某个类(class)的一个实例(instance),这里,'类'就是'类型'的同义词." 简 ...