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 ...
随机推荐
- SVR回归
1.python支持向量机回归svr预测 https://blog.csdn.net/u012581541/article/details/51181041 https://www.cnblogs.c ...
- php 操作excel
<?php $dir=dirname(__FILE__);//查找当前脚本所在路径 require $dir."/db.php";//引入mysql操作类文件 require ...
- python随机数的产生
导入 random模块 >>> import random 1. random.random random.random()用于生成一个0到1的随机浮点数: 0 <= n ...
- poj1236 Tarjan算法模板 详解
思想: 做一遍DFS,用dfn[i]表示编号为i的节点在DFS过程中的访问序号(也可以叫做开始时间)用low[i]表示i节点DFS过程中i的下方节点所能到达的开始时间最早的节点的开始时间.初始时dfn ...
- STL之set容器的总结
最近做了很多题型,都是用简单的STL就解决了,深刻的感觉到STL的伟大力量,但是本人在遇到问题的时候还是喜欢用常规的算法去解决问题,脑袋笨没办法,有时候根本想不到用STL去解决一些问题 往往都是砍了网 ...
- ajax对象的获取及其常用属性
ajax对象的获取及其常用属性 (1)什么是ajax asynchronous javascript and xml(异步的javascript和xml). 是一种用来改善用户体验的技术,其实质是利用 ...
- HUST——1110雪碧(简单DFS)
1110: 雪碧 时间限制: 1 Sec 内存限制: 128 MB 提交: 18 解决: 6 题目描述 杨神最近特别喜雪碧,他现在有两瓶,他大晚上的在街上走,他逢店加一倍(雪碧),逢摊吃大虾并喝一 ...
- [BZOJ3585][BZOJ3339]mex
[BZOJ3585][BZOJ3339]mex 试题描述 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. 输入 第一行n,m.第二行为n个数.从 ...
- BZOJ3991 [SDOI2015]寻宝游戏 【dfs序 + lca + STL】
题目 小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择一个村庄,瞬间转移到这个村庄,然后可以任意在地图的道路 ...
- 刷题总结——Tree2cycle(hdu4714 树形dp)
题目: A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 unit of ...