【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 ...
随机推荐
- springcloud(三):服务提供与调用
上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例. 案例中有三个角色:服务注册中心.服务提 ...
- css控制table的td宽度
今天发现即使设置table的td.th宽度,仍是不管用,是根据table的td的内容来适应宽度,导致其他的th.td丢失. 下图就是浏览器渲染的table,导致缺失"端口"这一列, ...
- 模板不存在:./xx 错误位置 FILE: LINE:110 (thinkphp上传至服务器后模板无法解析原因)
thinkphp上传至服务器后模板无法解析原因 前几日做好的响应式静态页面上传至虚拟空间,打开网址地址出现: 模板不存在:./App/Admin/View/Config/customerService ...
- StringBuffer类的使用
//简单记录下Java中StringBuffer类的基本功能使用public class Test01{ public static void main(String[] args){ //New一个 ...
- es6之Generator
1.Generator函数其实是一个封装了多个内部状态的状态机,执行它会返回一个遍历器对象,然后可以依次遍历Generator中的每一个状态,也就是分段执行,yield是暂停执行的标记,next恢复执 ...
- .net 非对称加密
后台 public class RSAHelper { /// <summary> /// RSA加密 /// </sum ...
- python爬虫从入门到放弃(五)之 正则的基本使用
什么是正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是 事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符",这个"规则字符" 来表达对 ...
- oracle创建用户四部曲
创建用户一般分四步: 第一步:创建临时表空间 第二步:创建数据表空间 第三步:创建用户并制定表空间 第四步:给用户授予权限 创建临时表空间 create temporary tablespace ho ...
- Myeclipse 配置多个tomcat
1.首先准备多个tomcat 命名为: tomcat-8087 tomcat-8088 tomcat-8089 2.修改对应的server.xml ①:修改关闭时端口,分别设为 8005 8 ...
- JavaScript设计模式_02_策略模式
在程序设计中,我们常常遇到这种情况,要实现某一个功能我们有很多种算法可以实现.这些算法灵活多样,而且可以随意互相替换.这种解决方案就是所谓的策略模式. /* * pre:策略模式 * 示例:公司计算奖 ...