字符串同构问题 字符串操作:数组计数字符个数问题
2015-10-11 21:04
267 查看
/article/1470030.html
题目概述:
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是否是同构的。(注意字符不仅是字母)
最简单的方法是通过计算每个字符出现的个数并且相应位置字母对应,但是两层循环肯定TLE。所以需要通过O(n)时间比较,采用的方法是:
eg: "aba" <> "baa" return false
关键代码:nums[s[i]]=t[i] numt[t[i]]=s[i] 再比较是否相同
nums['a']='b' numt['b']='a' (第一次出现)
nums['b']='a' numt['a']='b' (第一次出现)
nums['a']='b' <> t[2]='a' (第二次出现) return false
该方法技巧性比较强,当然如果你使用C++的映射就非常容易实现了。
C++推荐代码:
参考:/article/4999923.html
题目很简单,也很容易想到方法,就是记录遍历s的每一个字母,并且记录s[i]到t[i]的映射,当发现与已有的映射不同时,说明无法同构,直接return false。但是这样只能保证从s到t的映射,不能保证从t到s的映射,所以交换s与t的位置再重来一遍上述的遍历就OK了。
[cpp] view plaincopy
class Solution {
public:
bool isIsomorphic(string s, string t) {
if (s.length() != t.length()) return false;
map<char, char> mp;
for (int i = 0; i < s.length(); ++i) {
if (mp.find(s[i]) == mp.end()) mp[s[i]] = t[i];
else if (mp[s[i]] != t[i]) return false;
}
mp.clear();
for (int i = 0; i < s.length(); ++i) {
if (mp.find(t[i]) == mp.end()) mp[t[i]] = s[i];
else if (mp[t[i]] != s[i]) return false;
}
return true;
}
};
(By:Eastmount 2015-9-21 凌晨1点半 http://blog.csdn.net/eastmount/)
题目概述:
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是否是同构的。(注意字符不仅是字母)
最简单的方法是通过计算每个字符出现的个数并且相应位置字母对应,但是两层循环肯定TLE。所以需要通过O(n)时间比较,采用的方法是:
eg: "aba" <> "baa" return false
关键代码:nums[s[i]]=t[i] numt[t[i]]=s[i] 再比较是否相同
nums['a']='b' numt['b']='a' (第一次出现)
nums['b']='a' numt['a']='b' (第一次出现)
nums['a']='b' <> t[2]='a' (第二次出现) return false
该方法技巧性比较强,当然如果你使用C++的映射就非常容易实现了。
C++推荐代码:
参考:/article/4999923.html
题目很简单,也很容易想到方法,就是记录遍历s的每一个字母,并且记录s[i]到t[i]的映射,当发现与已有的映射不同时,说明无法同构,直接return false。但是这样只能保证从s到t的映射,不能保证从t到s的映射,所以交换s与t的位置再重来一遍上述的遍历就OK了。
[cpp] view plaincopy
class Solution {
public:
bool isIsomorphic(string s, string t) {
if (s.length() != t.length()) return false;
map<char, char> mp;
for (int i = 0; i < s.length(); ++i) {
if (mp.find(s[i]) == mp.end()) mp[s[i]] = t[i];
else if (mp[s[i]] != t[i]) return false;
}
mp.clear();
for (int i = 0; i < s.length(); ++i) {
if (mp.find(t[i]) == mp.end()) mp[t[i]] = s[i];
else if (mp[t[i]] != s[i]) return false;
}
return true;
}
};
(By:Eastmount 2015-9-21 凌晨1点半 http://blog.csdn.net/eastmount/)
相关文章推荐
- 信息安全系统设计基础第四周学习总结
- 【PAT】1092. To Buy or Not to Buy (20)
- 【Python】Learn Python the hard way, ex33 while循环
- 矩阵乘法
- 设计模式之桥接模式
- Java基础知识强化之IO流笔记46:IO流练习之 把文本文件中数据存储到集合中的案例
- C/C++ 字节序/位域(Bit-fields)
- String StringBuffer StringBuilder之间的区别
- web项目出现的异常
- Reversing Linked List
- 第二次练习——团队展示
- Rhel7 find查找属于特定用户或组的文件,并将其拷贝到指定目录
- 交互设计菜鸟稳健入门之路
- NSPredicate
- linux(centos)下ext4硬盘格式误删文件后的恢复(testdisk与photorec的使用)
- DirectX 11游戏编程学习笔记2: 文章1章Vector Algebra(向量代数)
- 来自clagnut的投影方法
- UI设计师必须了解:2015年十大移动端APP设计主流趋势
- Rhel7 创建swap分区, fstab自动开机挂载
- 处理select下拉框默认选中