【一天一道LeetCode】#205. Isomorphic Strings
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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,则返回true,否则返回false
解题思路:如题目给的例子,“egg”和“add”,e->a,g->d,通过这个映射关系可以得到add。
所以,很容易想到用hash表来解决这个问题。不过要注意: 不允许s中的两个不同字符对应t中的同一个字符。
即s = “ab”,t = “aa”,s中a和b不能同时对应t中的a
下面看代码:
class Solution {
public:
bool isIsomorphic(string s, string t) {
int len = s.length();
if(len==0) return true;
//必须要双向映射,避免出现一对多,多对一等情况
char hash[256];
char hash2[256];
memset(hash,' ',256*sizeof(char));
memset(hash2,' ',256*sizeof(char));
for(int i = 0 ; i < len ; i++){
if(hash[s[i]]==' '&&hash2[t[i]]==' '){//如果该组字符没有映射关系
hash[s[i]] = t[i];//建立映射关系
hash2[t[i]] = s[i];
}
else {
if(hash[s[i]]==t[i]&&hash2[t[i]]==s[i]) continue;
else return false;
}
}
return true;
}
};
【一天一道LeetCode】#205. Isomorphic Strings的更多相关文章
- [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
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- LeetCode 205. Isomorphic Strings (同构字符串)
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 ...
- 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 ...
- 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(同构的字符串)(string、vector、map)(*)
翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...
- Leetcode 205 Isomorphic Strings 字符串处理
判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...
随机推荐
- [BZOJ]2017省队十连测推广赛1
听学长说有比赛就随便打一打. A.普通计算姬 题目大意:给出一棵带权树,支持一下两种操作:1.修改一个点的权值:2.给出l,r,询问以点l为根的子树和.点l+1为根的子树和.点l+2为根的子树和--点 ...
- [bzoj4864][BeiJing 2017 Wc]神秘物质
来自FallDream的博客,未经允许,请勿转载,谢谢. 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研 ...
- Frame buffer分析 - fbmem.c【转】
转自:http://www.cnblogs.com/armlinux/archive/2012/03/01/2396753.html 45 struct fb_info *registered_fb[ ...
- 《Java技术》第一次作业
(一)学习总结 1.在java中通过Scanner类完成控制台的输入,查阅JDK帮助文档,Scanner类实现基本数据输入的方法是什么?不能只用文字描述,一定要写代码,通过具体实例加以说明. (1)使 ...
- JS多个对象添加到一个对象中
var obj1 = {"qq":10}; var obj2={"mm":2,"nn":3}; var obj3={"xx&quo ...
- Linux pip安装使用
pip安装使用详解 pip类似RedHat里面的yum,安装Python包非常方便.本节详细介绍pip的安装.以及使用方法. 1.pip下载安装 1.1 pip下载 1 # wget " ...
- NVIDIA Titan Xp Star Wars Collector's Edition显卡深度学习工作站 + Ubuntu17.10 + Tensorflow-gpu + Anaconda3 + Python 3.6 设置
为了能让 Tensorflow GPU 版本跑起来,我折腾了1个多星期. 总体参照 https://zhuanlan.zhihu.com/p/32118549 ,安装成功,但还是有不足的地方, 在此记 ...
- Eclipse创建Maven工程
Eclipse创建Maven工程: Eclipse: New -> Other -> Maven Project -> Next -> webapp -> Finish ...
- IPFS星际文件系统
IPFS星际文件系统(InterPlanetary File System)是去中心化文件系统,本文介绍IPFS节点软件系统安装,环境搭建等简介入门教程,及学习如何使用ipfs-api和Node.js ...
- tomcat连接池配置和使用
一种方法是在conf/context.xml文件中配置,配置oracle连接池的一个例子的context内容如下: <?xml version='1.0' encoding='utf-8'?&g ...