【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/isomorphic-strings/#/description
题目描述
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.
题目大意
看s到t的映射关系是不是一一映射的,s中不同的字符是不能映射到t的同一个字符的。
解题方法
字典保存位置
这个题可以看出来使用hashmap,但是,注意的一点是不要用位置++的方式,这样的话只统计了这个字符出现的次数,没有统计对应的位置,导致出错。比如aba
和baa
就会导致结果错误。因此,用到的是字符出现的位置+1的方式,保证能在hash位置处保存字符出现的位置。
public class Solution {
public boolean isIsomorphic(String s, String t) {
int[] m1 = new int[256];
int[] m2 = new int[256];
int len = s.length();
for(int i = 0; i < len; i++){
if(m1[s.charAt(i)] != m2[t.charAt(i)]){
return false;
}
m1[s.charAt(i)] = i + 1;
m2[t.charAt(i)] = i + 1;
}
return true;
}
}
字典保存映射
因为是一一映射关系,所以s到t和t到s的映射都要进行判断。最简单的方法就是使用两次判断,这样的话,我们可以分别看映射关系是不是一致的。
class Solution(object):
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
m = dict()
for i, c in enumerate(s):
if c in m:
if t[i] != m[c]:
return False
else:
m[c] = t[i]
m = dict()
for i, c in enumerate(t):
if c in m:
if s[i] != m[c]:
return False
else:
m[c] = s[i]
return True
既然想到了两次判断,那么我们应该也能想到,使用两个字典分别保存s到t的判断和t到s的判断。代码如下:
class Solution(object):
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
m = dict()
n = dict()
for i, c in enumerate(s):
if c in m and m[c] != t[i]:
return False
if t[i] in n and c != n[t[i]]:
return False
m[c] = t[i]
n[t[i]] = c
return True
日期
2017 年 5 月 15 日
2018 年 11 月 24 日 —— 周六快乐
【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)的更多相关文章
- [LeetCode] 205. Isomorphic Strings 解题思路 - Java
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [leetcode]205. Isomorphic Strings 同构字符串
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 ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
随机推荐
- java数组中Arrays类
使用Arrays类之后要先导入包,即在开头添加这行: import.java.util.Arrays 1,排序:Arrays.sort(数组名) 排序后为数组升序. 2,将数组转换成字符串:Array ...
- 内网穿透—使用 frp 实现内外网互通
前言 什么是内网穿透? 内网穿透,又叫 NET 穿透,是计算机用语.用通俗的说法就是你家里的个人电脑,可以直接被外网的人访问.例如你在公司,不通过远程工具,直接也可以访问到家里的电脑(本文章特指 we ...
- 初学者如何吃透一个Java项目
不少初学者朋友在学习Java过程中,会对着视频敲Java项目,其中遇到的BUG还能解决,但就是每次敲完一个项目,就感觉很空虚,项目里面的知识点感觉懂了但又好像没懂 这些朋友应该怎样才能掌握一个项目所用 ...
- 巩固javaweb的第二十四天
巩固内容: 提示用户信息 在验证失败之后通常需要提示用户错误信息,可以通过下面的代码完成: alert("地址长度大于 50 位!"); 当使用 alert 提示错误信息时,参数是 ...
- accommodate ~ ache
accommodate The accommodation reflex [反射] (or accommodation-convergence [会聚] reflex) is a reflex act ...
- day32 HTML
day32 HTML 什么是前端 只要是跟用户打交道的界面都可以称之为前端 # eg:电脑界面, 手机界面,平板界面, 什么是后端? eg:python, java,php,go, 不跟用户直接打交道 ...
- 剖析虚幻渲染体系(13)- RHI补充篇:现代图形API之奥义与指南
目录 13.1 本篇概述 13.1.1 本篇内容 13.1.2 概念总览 13.1.3 现代图形API特点 13.2 设备上下文 13.2.1 启动流程 13.2.2 Device 13.2.3 Sw ...
- Nginx LOCATOIN块配置
1 匹配模式优先级 location = /uri =开头表示精确匹配,只有完全匹配上才能生效. location ^~ /uri ^~ 开头对URL路径进行前缀匹配,并且在正则之前.无正则普通匹配( ...
- shell脚本 screen管理
一.简介 源码地址 日期:2018/4/12 介绍:使用screen来启动程序,这个脚本可以管理screen 效果图: 二.使用 适用:centos6+ 语言:中文 注意:请先写一个脚本来启动java ...
- centos部署golang环境
目录 一.简介 二.部署 一.简介 Go语言(或 Golang)起源于 2007 年,并在 2009 年正式对外发布.Go 是非常年轻的一门语言,它的主要目标是"兼具 Python 等动态语 ...