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.

此题目有时间限制,关键是如何优化时间。

我开始的做法是两个for循环,那么时间复杂度就是n的平方,但是它有一个测试用例,两个字符串特别长,于是就出现了“Time Limit Exceeded”。代码如下:

class Solution {

public:

 bool isIsomorphic(string s, string t) {
int len = s.length();
// 时间复杂度n平方,不满足题目要求。
for (size_t i = ; i < len; i++) {
for (size_t j = i + ; j < s.length(); j++) {
if ((s[i] == s[j] && t[i] != t[j]) || (s[i] != s[j] && t[i] == t[j])) {
return false;
}
}
}
return true;
}
};

上面的方法不行,那就必须要减少时间复杂度,最后我想了一个方法:使用一个<char, char>的map映射,for循环两个入参的每一个char,如果发现对应关系改变了,那么就说明两个字符串不是isomorphic的了。时间复杂度为O(n),代码如下:

class Solution {
public:
bool isIsomorphic(string s, string t) {
int len = s.length();
map<char, char> m;
map<char, char> m2;
for (size_t i = ; i < len; i++) {
if (m.find(s[i]) == m.end()) {
m[s[i]] = t[i];
}else if (m[s[i]] != t[i]) {
return false;
}
if (m2.find(t[i]) == m2.end()) {
m2[t[i]] = s[i];
}else if (m2[t[i]] != s[i]) {
return false;
}
}
return true;
}
};

205 Isomorphic Strings的更多相关文章

  1. 205. Isomorphic Strings - LeetCode

    Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...

  2. [leetcode]205. Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

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

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

  4. LeetCode 205 Isomorphic Strings

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

  5. Java for LeetCode 205 Isomorphic Strings

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  6. (String) 205.Isomorphic Strings

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  7. (easy)LeetCode 205.Isomorphic Strings (*)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. Java [Leetcode 205]Isomorphic Strings

    题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  9. [LeetCode] 205. Isomorphic Strings 解题思路 - Java

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

随机推荐

  1. 一些常用的Git命令

    1 删除文件 $ git rm filename 从已跟踪文件清单中移除,并连带从工作目录中删除指定的文件. 删除后,有2种选择: (1)如果确实要删除,则使用git commit提交. (2)如果不 ...

  2. Ubuntu 14.04 安装nVidia驱动后不能进入图形界面的恢复过程

    想要解决Ubuntu14.04的风扇不停的转的问题.由于ubuntu本身不支持双显卡切换,导致集显独显都处于开启状态,发热量和耗电量居高不下. 1. 安装驱动过程 参考[1]中的步骤,做了如下的操作. ...

  3. 实现快速迭代的引擎设计 - Capcom RE Engine的架构与实现

    [译]实现快速迭代的引擎设计 - Capcom RE Engine的架构与实现 ken hu· 6 天前 原文(日文):ラピッドイテレーションを実現するゲームエンジンの設計 CEDEC2016上的一个 ...

  4. Java数组,去掉重复值、增加、删除数组元素

    import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.HashSet; p ...

  5. JavaScript - javascript 中的 "||" 与 "&&" 的理解与灵活运

    你肯定见到过这样的代码:a = a||"xxx". 它其实就等价于下面三种形式的代码: a = a || "xxx"; 与: if (!a) { a = &qu ...

  6. sql like 时间

    and Convert(varchar(),TimeStamp,) like '%2013-09-06 09:46:03%'

  7. Knockout 新版应用开发教程之"text"绑定

    目的 DOM元素显示文本的值是你传递的参数,前提是text先绑定到该元素上 典型的常用元素 <span>或者<em>习惯性的用来显示文本,但是在技术上来说你可以用任何元素的. ...

  8. Android学习笔记之SoftReference软引用...

    PS:其实这一篇和上一篇很类似,都是为了解决内存不足(OOM)这种情况的发生... 学习内容: 1.对象的引用类....   最近也是通过项目中知道了一些东西,涉及到了对象的引用类,对象的引用类分为多 ...

  9. [ShortCut] IE10快捷键

    适用范围: Windows 8 操作步骤: 1.快速输入网址: “Ctrl+L”.“F4”:在IE10下按下“Ctrl+L”快捷键,可以直接将光标转到浏览器地址栏(注:地址栏中的网址会被选中),可以直 ...

  10. Spring基础——小的知识点

    一.整合多个配置文件 在 Spring Config 文件中,可以使用 <import> 标签将别的配置文件引入到一个文件中,进行配置文件的集成.该标签和 <bean> 标签同 ...