LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
翻译
给定两个字符串s和t,决定它们是否是同构的。
假设s中的元素被替换能够得到t,那么称这两个字符串是同构的。
在用一个字符串的元素替换还有一个字符串的元素的过程中。所有字符的顺序必须保留。
没有两个字符能够被映射到同样的字符,但字符能够映射到该字符本身。
比如,
给定“egg”,“add”,返回真。
给定“foo”。“bar”,返回假。
给定“paper”,“title”,返回真。
批注:
你能够假设s和t有同样的长度。
原文
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 example,
Given "paper", return "01023".
Given "foo", return "011".
Given "isomorphic", return "0123245607".
于是就将这个功能给实现了:
vector<int> getVecOrder(string str) {
map<char, int> strM;
int index = 0;
vector<int> strVec;
for (int i = 0; i < str.size(); ++i) {
auto iter = strM.find(str[i]);
if (iter == strM.end()) {
strM.insert(pair<char, int>(str[i], index));
strVec.push_back(index);
index += 1;
}
else {
strVec.push_back(strM[str[i]]);
}
}
return strVec;
}
这里用map来保存每一个字符和索引的键值对,索引用index来表示。索引从0開始。
最后的数字序列用vector来保存。
循环遍历整个字符串,每次在map中寻找一个字符,假设没有找到,则将其和相应的index加入进去,假设已经存在,就将该字符的索引从map中获取出来并加入到vector中。
有了这个模块函数,解起题来就轻而易举咯:
bool isIsomorphic(string s, string t) {
vector<int> v_s = getVecOrder(s), v_t = getVecOrder(t);
for (int i = 0; i < v_s.size(); ++i) {
if (v_s[i] != v_t[i]) return false;
}
return true;
}
由于字符串的长度题目说了是等长的,所以vector的长度肯定也是相等的了。
updated at 2016/09/05
同理,也能够改成例如以下所看到的的 Java 代码~
private ArrayList getArrayOrder(String str) {
HashMap<Character, Integer> strM = new HashMap<>();
int index = 0;
ArrayList order = new ArrayList(str.length());
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (strM.containsKey(c)) {
order.add(strM.get(c));
} else {
strM.put(c, index);
order.add(index);
index += 1;
}
}
return order;
}
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length())
return false;
ArrayList s0 = getArrayOrder(s), t0 = getArrayOrder(t);
for (int i = 0; i < s0.size(); i++)
if (s0.get(i) != t0.get(i))
return false;
return true;
}
代码
class Solution {
public:
vector<int> getVecOrder(string str) {
int len = str.size();
map<char, int> strM;
int index = 0;
vector<int> strVec;
for (int i = 0; i < len; ++i) {
auto iter = strM.find(str[i]);
if (iter == strM.end()) {
strM.insert(pair<char, int>(str[i], index));
strVec.push_back(index);
index += 1;
}
else {
strVec.push_back(strM[str[i]]);
}
}
return strVec;
}
bool isIsomorphic(string s, string t) {
vector<int> v_s = getVecOrder(s), v_t = getVecOrder(t);
for (int i = 0; i < v_s.size(); ++i) {
if (v_s[i] != v_t[i]) return false;
}
return true;
}
};
LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、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同构字符串
哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查 ...
- LeetCode 205. Isomorphic Strings (同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 205 Isomorphic Strings 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...
- Leetcode 205 Isomorphic Strings 字符串处理
判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- [LeetCode] 205. Isomorphic Strings 解题思路 - Java
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Java for LeetCode 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 ...
随机推荐
- 03-python进阶-爬虫入门-正则
[urllib and urllib2] 这是两个python的网络模块 内置的 提供很好的网络访问的功能. #!coding:utf-8 import urllib2 res = urllib2.u ...
- 在Asp.net MVC中应该怎样使用Spring.Net
简单工厂 专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类或接口.简单工厂模式又称为静态工厂方法(Static Factory Method)模式,属于类的创建型模式,通常根据一 ...
- 【Luogu】P2447外星千足虫(高斯消元)
题目链接 高斯消元解%2意义下的方程,Bitset优化一下. 在消的过程中就能顺便把有解的第一问求出来,记录一下访问过的最大行. #include<cstdio> #include< ...
- C# Settings.settings的用处
1.定义 在Settings.settings文件中定义配置字段.把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改.可以使用数据网格视图,很方便: 2.读取配置值 t ...
- 表单编码 appliation/x-www-form-urlencoded 与 multipart/form-data 的区别
当表单使用POST方法时,表单数据提交到服务器端之前有两种编码类型可供选择.默认编码类型为 application/x-www-form-urlencoded,此时所有非字母数字类型的字符都需要转换为 ...
- 换教室(bzoj 4720)
Description 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节 课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节内容相同的 ...
- HDU 6231 (二分+双指针)
题意:给一个长度为n的数组,问在由这个数组的所有的区间第k小组成B数组中,第m大元素是多少 解法:这题较难的地方在于转化思维.如果去求所有区间的第k小,最坏复杂度是O(n*n)肯定超时. 这题正确的解 ...
- 【Visual Studio】Windows program compatibility mode is on, turn it off……
[问题描述]Windows 10上安装 Visual Studio Ultimate 2013,出现下面错误: [解决方案]将.iso文件解压到硬盘再安装.
- C语言处理json字符串
JSON语法说明 先来看一个简单的JSON 1 { 2 "stars": [ 3 { 4 "name": "Faye", 5 "a ...
- Yii关联查询(转载)
原文链接:http://keshion.iteye.com/blog/1607994 一.多表关联的配置 在我们使用 AR 执行关联查询之前,我们需要让 AR 知道一个 AR 类是怎样关联到另一个的. ...