【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 ...
随机推荐
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- TCP流量控制和拥塞控制
TCP的流量控制 所谓的流量控制就是让发送方的发送速率不要太快,让接收方来得及接受.利用滑动窗口机制可以很方便的在TCP连接上实现对发送方的流量控制.TCP的窗口单位是字节,不是报文段,发送 ...
- Java IO流学习总结(1)
Java IO流学习总结 Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本 ...
- 【Netty】ChannelHandler和ChannelPipeline
一.前言 前面学习了Netty的ByteBuf,接着学习ChannelHandler和ChannelPipeline. 二.ChannelHandler和ChannelPipeline 2.1 Cha ...
- 利刃 MVVMLight 10:Messenger 深入
1.Messager交互结构和消息类型 衔接上篇,Messeger是信使的意思,顾名思义,他的目是用于View和ViewModel 以及 ViewModel和ViewModel 之间的消息通知和接收. ...
- 机器学习技法课之Aggregation模型
Courses上台湾大学林轩田老师的机器学习技法课之Aggregation 模型学习笔记. 混合(blending) 本笔记是Course上台湾大学林轩田老师的<机器学习技法课>的学习笔记 ...
- js继承之借用构造函数继承
我的上一篇文章介绍了,原型链继承模式.但是单纯的原型链模式并不能很好地实现继承. 一.原型链的缺点 1.1 单纯的原型链继承最大的一个缺点,来自于原型中包含引用类型的值. 本来,我们没有通过原型链实现 ...
- git底层原理(一)
1.git仓库的初始化: 输入git init指令,会看到在当前空目录下创建了一个.git隐藏文件夹,这个就是git实现一切版本管理的关键.进入到.git目录下,里面包含三个文件(config/des ...
- 【小练习06】HTML+CSS--教学大讲堂
要求实现如下效果图: 代码演示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...
- PHP文件操作,多行句子的读取,file()函数,file_get_contents()函数,file_put_contents()函数,is_file,统计网站pv (访问量),文件的复制 copy,文件重命名 rename,删除文件 unlink
php中添加utf-8: header("Content-type:text/html;charset='UTF-8'"); 文件操作步骤: 1.在同一目录下建立一个file.tx ...