C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3770 访问。
给定两个字符串 s 和 t,判断它们是否是同构的。
如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。
所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
输入: s = "egg", t = "add"
输出: true
输入: s = "foo", t = "bar"
输出: false
输入: s = "paper", t = "title"
输出: true
说明:你可以假设 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.
Input: s = "egg", t = "add"
Output: true
Input: s = "foo", t = "bar"
Output: false
Input: s = "paper", t = "title"
Output: true
Note:You may assume both s and t have the same length.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3770 访问。
public class Program {
public static void Main(string[] args) {
var s = "egg";
var t = "add";
var res = IsIsomorphic(s, t);
Console.WriteLine(res);
Console.ReadKey();
}
private static bool IsIsomorphic(string s, string t) {
var dic = new Dictionary<int, int>();
for(var i = 0; i < s.Length; i++) {
if(!dic.ContainsKey(s[i])) {
if(dic.ContainsValue(t[i])) return false;
dic[s[i]] = t[i];
}
}
for(var i = 0; i < s.Length; i++) {
if(dic[s[i]] != t[i]) return false;
}
return true;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3770 访问。
True
分析:
该题解法参考 C#LeetCode刷题之#290-单词模式(Word Pattern)。
显而易见,以上算法的时间复杂度为: 。
C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)的更多相关文章
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- LeetCode 205:同构字符串 Isomorphic Strings
题目: 给定两个字符串 s 和 *t*,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 *t* ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字 ...
- C#LeetCode刷题之#859-亲密字符串(Buddy Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3973 访问. 给定两个由小写字母构成的字符串 A 和 B ,只要 ...
- C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
- C#LeetCode刷题之#344-反转字符串(Reverse String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...
- C#LeetCode刷题之#541-反转字符串 II(Reverse String II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3951 访问. 给定一个字符串和一个整数 k,你需要对从字符串开头 ...
- C#LeetCode刷题之#443-压缩字符串(String Compression)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3943 访问. 给定一组字符,使用原地算法将其压缩. 压缩后的长度 ...
- [Swift]LeetCode205. 同构字符串 | Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- toad for oracle 小技巧
在SQL*LOADER 工具上(或者称为SQLLDR,读为:“sequel loader”),因为它仍然是装载数据的主要方法,SQLLDR 能够在极短的时间内装 载庞大数量的数据. 我也是初使用,理解 ...
- 学Python常用数据结构之字典
迄今为止,我们已经为大家介绍了Python中的三种容器型数据类型,但是这些数据类型还不足以帮助我们解决所有的问题.例如,我们要保存一个人的信息,包括姓名.年龄.体重.单位地址.家庭住址.本人手机号.紧 ...
- OpenLDAP 2.4.44 安装部署避坑指南
写在前面: 有关openLDAP的部署文档在网上随意能找到很多,但是最近用到才发现网上的教程多数是旧版的用法,例如"/etc/openldap/slapd.conf “早已弃用,更有甚者直接 ...
- static关键字和final关键字
static关键字和final关键字 static(静态) 作用 用来修饰属性.方法.代码块.内部类 static修饰属性 表示静态变量(类变量) 按是否使用static修饰,属性的分类 静态属性 当 ...
- 题解 洛谷 P4546 【[THUWC2017]在美妙的数学王国中畅游】
首先发现有连边和删边的操作,所以我们肯定要用\(LCT\)来进行维护. 接下来考虑如何进行\(LCT\)上的信息合并. \(f=1\),则函数为\(f(x)=sin(ax+b)\) \(f=2\),则 ...
- C++头文件居然可以这么打!!!! 长见识了!!!
返回主页 longdie 这人,生于天,立于地,为的就是顶天立地. 未来的答案早已被宇宙计算好了,人类自出现,答案就在那里,人类灭亡了,答案也在那里,,但是人活着,不就是为了看看未来发生了什么吗?如果 ...
- 《闲扯Redis七》Redis字典结构的底层实现
一.前言 上节<闲扯Redis六>Redis五种数据类型之Hash型 中说到 Hash(哈希对象)的底层实现有: 1.ziplist 编码的哈希对象使用压缩列表作为底层实现 2.hasht ...
- 【Laravel】使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
一.安装配置 使用Composer安装依赖: composer require maatwebsite/excel 发布配置(可选): php artisan vendor:publish --pro ...
- 第二部分_Mac技巧
原文是"池建强"的微信文章,公众号为"MacTalk" 第五十一天 mdfind是一个非常灵活的全局搜索命令,类似Spotlight的命令行模式,可以在任何目录 ...
- Hexo 静态博客指南:建站教程(上)
本文最初发布于我的个人博客Bambrow's Blog,采用 BY-NC-SA 许可协议,转载请注明出处.若有后续更新,将更新于原博客.欢迎去我的博客阅读更多文章! 本文详细记录一下站点建立过程,以便 ...