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)。
显而易见,以上算法的时间复杂度为: 。
最新文章
- 1264: [AHOI2006]基因匹配Match
- js的extend和fn.extend使用
- ubuntu-14.04.x-desktop-amd64.iso:安装Oracle11gR2
- Tab指示符——Indicator
- 经典JSP数据库连接(ORACLE、SQL Server、MySQL)
- 150 Opening ASCII mode data connection. FTP连接的PASV和PORT方式
- java转义字符
- 在制作joomla模板过程中遇到的问题
- 使用struts2+hibernate的增、删、改、查构架简单的学生管理系统
- -_-#QUOTA_EXCEEDED_ERR: DOM Exception 22
- (coco2d-x初学)xcode5.0安装 cocos2d-x2.2.0
- c/c++ 算法之快速排序法 冒泡排序法,选择排序法,插入排序法
- bzoj1121
- 查询操作 -- Django从入门到精通系列教程
- shell(3)拼写检查与词典操作
- BackgroundWorker Class Sample for Beginners
- python第一类对象,闭包,迭代器
- L228 the complicated issue of equality: non-disabled actors play disabled roles
- 搭建Hive 2.1.1 基于Hadoop 2.6.1 和 Ubuntu 16.0.4 记录
- UOJ117. 欧拉回路【欧拉回路模板题】