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 ...
随机推荐
- Mime类型与文件后缀对照表及探测文件MIME的方法
说明:刚刚写了一篇<IHttpHandler的妙用(2):防盗链!我的资源只有我的用户才能下载>的文章,网址:http://blog.csdn.net/zhoufoxcn/archive/ ...
- ogre3D学习基础10 -- 键盘控制与鼠标控制(缓冲控制)
带缓冲的鼠标.键盘操作,这里的鼠标.按键事件会被各自的监听器捕获.其中OIS中定义的两个类MouseListener,KeyListener负责对事件的处理.我们需要使用这两个类的接口. 当一个键被按 ...
- 《Effective Java》笔记 :(一)创建和销毁对象
一 .考虑用静态工厂方法代替构造器 1. 静态工厂方法与设计模式中的工厂方法模式不同,注意不要混淆 例子: public static Boolean valueOf(boolean b){ retu ...
- spring AOP详解〇
AOP正在成为软件开发的下一个圣杯.使用AOP,你可以将处理aspect的代码注入主程序,通常主程序的主要目的并不在于处理这些aspect.AOP可以防止代码混乱. 为了理解AOP如何做到这点,考虑一 ...
- 【Luogu】P2765魔术球问题(没看懂的乱搞)
题目链接 这题……讲道理我没看懂. 不过我看懂题解的代码是在干嘛了qwq 题解是zhaoyifan的题解 然后……我来讲讲这个题解好了. 题解把值为i的球拆成了两个,一个编号是i*2,一个编号是i*2 ...
- eclipse testng插件安装
1.安装Testng 在Eclipse中,点击Help→Install new software→点击Add,在location中输入http://beust.com/eclipse ,选择TestN ...
- inux读取ISO文件或是光驱的方法--挂载
inux读取ISO文件或是光驱的方法--挂载 首先在虚拟机选项的设置里设置CD/DVD选项,勾选:Connect at power on 再在连接中选择:Use ISO image file即选择镜像 ...
- springboot中的controller注解没有生效
springboot中的controller注解没有生效 , 启动的Application类没有在controller的父目录或同级目录
- pexpect模块
pexpect用来启动子程序,使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的python模块,当然我们可以使用他来做ssh登陆,ssh模块登陆还有一个基于python实现远程连接,用于s ...
- Win32 绘制RGB三原色图案
以前看到三原色的图案,一直很好奇是如何画出来.后来终于搞清楚了,其实很简单,实际上就是RGB三个分量的"位与"运算. 下面给出Win32绘制三原色图案的例子,特此记录在此: #in ...