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 ...
随机推荐
- C# 接口的隐式与显示实现
隐式实现的话实现的方法属于实现的类的,可以直接通过类的对象访问,显式实现的话方法是属于接口的,可以看成是寄托在类中实现的,访问这些方法时要先把对象转换成接口对象,然后通过接口对象调用 一般来讲显式实现 ...
- swift 如何删除subviews
scrollView.subviews.map { (var view) -> () in if (view is UIButton) { view.removeFromSuperview() ...
- Embeding Python & Extending Python with FFPython
Introduction ffpython is a C++ lib, which is to simplify tasks that embed Python and extend Python. ...
- jade模板引擎学习笔记(WebsStorm9.0.3+ nodejs+express+jade)
jade环境搭建 jade标签写法 jade注释 jade添加类名.id.属性 jade添加脚本,css jade变量 jade多行文本显示 jade流程代码:for,each,while jade流 ...
- 用Wireshark简单分析HTTP通信
我们都学过TCP,HTTP的相关概念,本文借助协议分析工具Wireshark,让大家对一些概念眼见为实,权当温故而知新. 场景: 在Client(10.239.196.211)上通过web brows ...
- 让Windows 7变成WIFI热点
360要推便携路由器,个人觉得其主要目的是盯住了用户无线设备上的信息.因为如果用户移动设备都通过它这个路由器走的话,未加密的数据全部在他掌控之中. 其实Windows 7以上的系统是非常容易建立无线热 ...
- SLAM拾萃(3):siftGPU
前言 本周博客我们给大家介绍一下SiftGPU.由于特征匹配是SLAM中非常耗时间的一步,许多人都想把它的时间降至最短,因此目前ORB成了非常受欢迎的特征.而老牌SIFT,则一直给人一种“很严谨很精确 ...
- mongodb备份恢复
注意:在备份文件存放目录的选择上有这样一个条件,文件存放目录不管有多深,都只能有它一个文件:(即除了备份文件之外只能存在文件夹,这个条件约束其整个目录树) 数据备份 : /mongodump --ho ...
- rails从4.0.2降到3.2.9
初学ruby和rails,想和教程同步,把rails的版本降下来.从4.0.2降到3.2.9 [lucas@lucas ~]$ rails -v Rails 4.0.2 尝试了 sudo gem un ...
- shell的重定向
>file 将file文件重定向为输出源,新建模式,可以将正确的结果输出到file文件 >>file 将file文件重定向为输出源,追加模式 <file 将file文件重定 ...