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.
此题目有时间限制,关键是如何优化时间。
我开始的做法是两个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的更多相关文章
- 205. Isomorphic Strings - LeetCode
		
Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...
 - [leetcode]205. Isomorphic Strings 同构字符串
		
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
 - 【刷题-LeetCode】205. Isomorphic Strings
		
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
 - LeetCode 205 Isomorphic Strings
		
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
 - Java for LeetCode 205 Isomorphic Strings
		
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
 - (String) 205.Isomorphic Strings
		
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
 - (easy)LeetCode  205.Isomorphic Strings (*)
		
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
 - Java [Leetcode 205]Isomorphic Strings
		
题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
 - [LeetCode] 205. Isomorphic Strings 解题思路 - Java
		
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
 
随机推荐
- LoadRunner AJAX TruClient协议Tips and Tricks
			
LoadRunner AJAX TruClient协议Tips and Trickshttp://automationqa.com/forum.php?mod=viewthread&tid=2 ...
 - spark  MySQL  jar 包
			
/** * Created by songcl on 2016/6/24. */ import java.sql.DriverManager //val sqlContext = new org.ap ...
 - ffmpeg中的sws_scale算法性能测试
			
经常用到ffmpeg中的sws_scale来进行图像缩放和格式转换,该函数可以使用各种不同算法来对图像进行处理.以前一直很懒,懒得测试和甄 别应该使用哪种算法,最近的工作时间,很多时候需要等待别人.忙 ...
 - c与c++中的extern const的区别和联系
			
最近复习c++,发现了这个东西. c语言里面,我们在一个.c文件中用const定义了一个全局变量后,可以在另一个.c文件中用extern const来引用,但在c++中在链接的时候会报undefine ...
 - Python--matplotlib绘图可视化知识点整理
			
from:https://segmentfault.com/a/1190000005104723 本文作为学习过程中对matplotlib一些常用知识点的整理,方便查找. 强烈推荐ipython无论你 ...
 - PE渲染引擎 二
			
增加了DOF
 - webpack+vue-loader 在单独.vue组件中使用sass-loader编译sass报错问题not a valid Win32 applictation
			
如果webpack配置没有问题,在vue文件中编译sass/scss报上面的错误,大概是由于node-sass安装失败,重新卸载安装, 在国内安装node-sass失败的话,可以使用淘宝镜 ...
 - OP和DBA相关的一些有用资源
			
最近国外blog上看到的一片资源分享博文,精而全,于是转帖分享 Must-Read Books List First of all, I would like to share a list of b ...
 - 手把手教你从购买vps到搭建一个node服务器
			
要准备什么? 1.5刀 2.最好有FQ软件(可以用蓝灯) let's Go! 一.vps购买 vps可以选择digital ocean(do) 链接 ,由于是外国网站,响应比较慢,所以最好翻个墙. g ...
 - 操作ACCESS数据库注意事项
			
以下问题都是容易忽略,但却不容易找出问题的所在,让我头疼不少,故在此列出,即是一个总结,同样也给其他人参与! 1.使用参数形式执行SQL命令时,参数数组需与在SQL语句中参数名出现的位置及名称必须完全 ...