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 ...
随机推荐
- Run same command on all SQL Server databases without cursors
original: https://www.mssqltips.com/sqlservertip/1414/run-same-command-on-all-sql-server-databases-w ...
- 让Extjs EditorGridPanel 编辑时支持方向键
在用 extjs editorgridpanel 进行输入编辑的时候, 默认情况下只支持使用 tab 键可以实现焦点切换, 如果想让editorgridpanel 在编辑时通过方向键来实现焦点跳转切换 ...
- ubuntu下搭建lamp
一.使用apt-get方式为Ubuntu安装PHP+MYSQL+Apache 分别执行如下命令: (1)安装MYSQL sudo apt-get install mysql-server ...
- 译:在C#中使用LINQ To SQL
译文出处:http://www.codeproject.com/Tips/871938/LINQ-To-SQL-Using-Csharp 今天在这个话题中,我给大家分享一个在c#编程中非常有趣和十分有 ...
- Android开发环境搭建相关文章列表(转载)
Android开发虽然有所了解,但是一直没有搭建开发环境去学习,Android的更新速度比较快了,Android1.0是2008年发布的,截止到目前为止Android已经更新Android5.0.1, ...
- [转]Android开发最佳实践
——欢迎转载,请注明出处 http://blog.csdn.net/asce1885 ,未经本人同意请勿用于商业用途,谢谢—— 原文链接:https://github.com/futurice/and ...
- BW对应后台表[转]
数据源对应后台表 (2012-01-04 20:08:57) 转载▼ 标签: 杂谈 分类: SAP MM Data Sources Tables Purchasing 2LIS_02_SCL EKKO ...
- Sublime Text3 插件集合
下载地址:http://download.csdn.net/detail/yinluhui/9029791 [包含的插件有: AndyJS2.BracketHighlighter.emmet-subl ...
- java攻城狮之路(Android篇)--BroadcastReceiver&Service
四大组件:activity 显示. contentProvider 对外暴露自己的数据给其他的应用程序.BroadcastReceiver 广播接收者,必须指定要接收的广播类型.必须明确的指定acti ...
- ruby -- 进阶学习(十一)配置解决production环境下无法加载css或js
最近配置production环境,找了好几份文档,从傻逼到苦逼~~终于配置成功~~@_@!!! 首先,先加载以下几个插件: # Use Uglifier as compressor for JavaS ...