【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.
提示:
此题的关键是要保证s与t中每一个字符之间都是一一对应的关系(即不能出现一对多或多对一的情况)。我们可以维护两张哈希表,一张保存s到t的映射关系,另一张保存t到s的映射关系。如果用C++的unordered_map是可以实现的,但是对于这个问题,由于可以确定输入的字符串中所有可能出现的字符不会超过128种,因此用数组代替unordered_map可以获得性能上的提升。
代码:
class Solution {
public:
bool isIsomorphic(string s, string t) {
if (s.length() == ) return true;
char dic_s[] = {}, dic_t[] = {};
for (int i = ; i < s.length(); ++i) {
if (dic_s[s[i]] == && dic_t[t[i]] == ) {
dic_s[s[i]] = t[i];
dic_t[t[i]] = s[i];
} else {
if (dic_s[s[i]] != t[i] || dic_t[t[i]] != s[i]) {
return false;
}
}
}
return true;
}
};
【LeetCode】205. Isomorphic Strings的更多相关文章
- 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...
- 【刷题-LeetCode】205. Isomorphic Strings
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
- 【一天一道LeetCode】#205. Isomorphic Strings
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【leetcode❤python】 205. Isomorphic Strings
#-*- coding: UTF-8 -*- #转换法class Solution(object): def isIsomorphic(self, s, t): "&qu ...
- Baozi Leetcode Solution 205: Isomorphic Strings
Problem Statement Given two strings s and t, determine if they are isomorphic. Two strings are isomo ...
- 【LeetCode】859. Buddy Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】43. Multiply Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- 【LeetCode】43. Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
随机推荐
- Redis学习-String
命令 描述 复杂的 返回值 SET key value [EX seconds] [PX milliseconds] [NX|XX] 将字符串值value关联到key.如果key已经持有其他值, ...
- Jlink下载问题
在使用Jlink SWD模式进行下载的时候遇到了无法下载的问题. SWD模式下,共有4跟线,VCC.GND.SWCLK.SWDIO JTAG标准接口如下图所示: 一般情况下,目标板卡的 debug V ...
- 关于XML(可扩展标记语言)的基础知识与写法------2017-05-18
XML(Extensible Markup Language) HTML:超文本标记语言,主要用来展示 XML:可扩展标记语言,用来做数据传输XML特点: 1.树状结构,有且只有一个根 2.标签名 ...
- 重新绑定ItemsSource先设置ItemsSource = null;的原因
即报错信息为:在使用 ItemsSource 之前,项集合必须为空. 原因:Items和ItemSource,只能有一个生效,想用其中一个,另一个必须是空. 重新绑定ItemSource,虽然 ...
- 软件工程期末考试 AHNU
1. 数据流图:一种图形化技术,它描绘信息流和数据从输入移动到输出的过程中所经受的变换.在数据流图中没有任何具体的物理部件,它只是描绘数据在软件中流动和被处理的逻辑过程,是系统逻辑功能的图形 ...
- WinForm笔记
Hi All, 分享一个学WinForm时的笔记: 1. 关键字 partial:是部分类,允许将一个类放在多个文件当中. 2. MessageBox()类相当于Console.WriteLine() ...
- inux中shell变量$#,$@,$0,$1,$2的含义
转自:http://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量 ...
- Bottle源码阅读笔记(一):WSGI
前言 Bottle是一个Python Web框架.整个框架只有一个文件,不到4k行的代码,没有Python标准库以外的依赖,却包含了路由.模板和插件等Web框架常用功能.通过阅读Bottle源码来了解 ...
- nodeJS之事件events
前面的话 events模块是node的核心模块,几乎所有常用的node模块都继承了events模块,比如http.fs等.本文将详细介绍nodeJS中的事件机制 EventEmitter 多数 Nod ...
- HashMap集合
HashMap集合特点(用法与特点类似于HashSet集合): 1.无序,不允许重复(无序指元素顺序与添加顺序不一致): 2.底层数据结构是哈希表 3.HashMap内部对"键"用 ...