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 ...
随机推荐
- 理解js中的原型,原型对象,原型链
目录 理解原型 理解原型对象 实例属性与原型属性的关系 更简单的原型语法 原型的动态性 原型链 理解原型 我们创建的每一个函数都有一个prototype(原型)属性,这个属性是一个指针,指向一个对象, ...
- Ethical Hacking - GAINING ACCESS(7)
Server Side Attacks - NEXPOSE NeXpose is a vulnerability management framework, it allows us to disco ...
- Python Ethical Hacking - VULNERABILITY SCANNER(6)
EXPLOITATION - XSS VULNS EXPLOITING XSS Run any javascript code. Beef framework can be used to hook ...
- presto 转换静态catlog为动态catlog
近年来,基于hadoop的sql框架层出不穷,presto也是其中的一员.从2012年发展至今,依然保持年轻的活力(版本迭代依然很快),presto的相关介绍,我们就不赘述了,相信看官多对presto ...
- Python对列表去重的各种方法
一.循环去重 二.用 set() 去重 1.set()对list去重 2.list 是有序的,用 sort() 把顺序改回来 三.利用 dict 的属性来去重 1.用 dict 的 fromke ...
- form表单两种提交方式的不同
我们在使用<Form>表单的时候,最常用的提交方式就是Get和Post.我们都知道这两种方式最大的差别就是安全性,除此之外,它们还有哪些其他的区别,你知道吗? 在<Form& ...
- 使用faker生成测试数据
需要先安装faker模块,pip install faker 导入模块中的Faker类:from faker import Faker 实例化faker = Faker() print('姓名相关') ...
- Android集成JPush(极光推送)
目前只是简单的集成 1.在极光推送官网注册用户 2.创建应用 3.配置包名,获得APPKEY 去设置 输入应用包名 确定然后返回查看APPKEY 3.在应用中集成极光推送 用的jcenter自动集成的 ...
- Redis之Redis的数据类型
1.Redis的数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(无序集合)及ZSet(有序集合) 2.String(字符串) ...
- Oracle帐户被锁了,如何解锁
原文链接:https://jingyan.baidu.com/article/25648fc144b76b9191fd0087.html 背景:Oracle帐户在密码被连续输入错误3次的情况下就会锁定 ...