题目:

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的更多相关文章

  1. 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...

  2. 【刷题-LeetCode】205. Isomorphic Strings

    Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...

  3. 【一天一道LeetCode】#205. Isomorphic Strings

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  4. 【leetcode❤python】 205. Isomorphic Strings

    #-*- coding: UTF-8 -*- #转换法class Solution(object):    def isIsomorphic(self, s, t):        "&qu ...

  5. Baozi Leetcode Solution 205: Isomorphic Strings

    Problem Statement Given two strings s and t, determine if they are isomorphic. Two strings are isomo ...

  6. 【LeetCode】859. Buddy Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  7. 【LeetCode】43. Multiply Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【leetcode】415. Add Strings

    problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...

  9. 【LeetCode】43. Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

随机推荐

  1. JAVA并发编程实战---第二章:线程安全性

    对象的状态是指存储在状态变量中的数据.对象的状态可能包括其他依赖对象的域.例如HashMap的状态不仅存储在HashMap本身,还存储在许多Map.Entry对象中.对象的状态中包含了任何可能影响其外 ...

  2. 最近一些朋友问我,临近快毕业了专业不对口,想转行看到IT行业就业前景不错,但是编程语言众多不了解,不知道哪门语言能够快速入门掌握,短期能让我找到工作

    我做互联网前端后台开发也有四年多了,一路走过来,累并快乐着.快乐比艰辛更多,源自我的兴趣驱动.初中的一个偶然的机会我接触到了计算机,从那个时候就喜欢上开始经常到网吧上网.那个时候我对计算机领域的认识是 ...

  3. 学习MVC之租房网站(十)-预约和跟单

    在上一篇<学习MVC之租房网站(九)-房源显示和搜索>完成了房源的显示.检索,然后是用户的预约看房,以及后台操作员对预约看房的跟单操作. 预约看房仅有将预约信息保存到对应表的操作,预约表有 ...

  4. 接口测试-jmeter

    一.jmeter下载安装 1)安装jdk(见博文<windows上安装jdk>) 2)在jmeter官网下载  http://jmeter.apache.org/download_jmet ...

  5. 必知的 15 个jQuery小技巧(干货)

    jQuery小技巧(干活) 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. $('a.top').click(function(){ ...

  6. 014 一对多关联映射 单向(one-to-many)

    在对象模型中,一对多的关联关系,使用集合来表示. 实例场景:班级对学生:Classes(班级)和Student(学生)之间是一对多的关系. 多对一.一对多的区别: 多对一关联映射:在多的端加入一个外键 ...

  7. spring service层单元测试

    service层测试较简单,目前大多数测试主要是针对public方法进行的.依据测试方法划分,可以分为两种:基于mock的隔离测试和基于dbunit的普通测试. mock隔离测试 配置pom.xml ...

  8. Tomcat 连接池详解

    (转) JDBC 连接池 org.apache.tomcat.jdbc.pool 是Apache-Commons DBCP连接池的一种替换或备选方案. 那究竟为何需要一个新的连接池? 原因如下: Co ...

  9. 不知道Linux的mysql的root密码

    用了好几次了这个方法.记一下: 1.停止Mysql /etc/init.d/mysql stop 或者(根据安装配置的位置不同,而不同) /etc/init.d/mysqld stop 2.进入Mys ...

  10. 互联网“剁手”新方向,VR全景购物忙——全景智慧城市常诚

    随着VR和AR技术的兴起,各行各业都在寻求VR+的对接方式,除了游戏和社交平台,另一大对VR有着浓厚兴趣的就是电商平台了,阿里.京东等电商巨头纷纷成立VR事业部,如何让亿万用户在VR中愉快的买买买,已 ...