问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)。

显而易见,以上算法的时间复杂度为: 

最新文章

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

热门文章

  1. Python numpy 浮点数精度问题
  2. threadLocal源码土话解说
  3. Eclipse点击空格总是自动补全代码怎么办,如何自动补全代码,代码提示
  4. 题解 洛谷 P2287 [USACO07NOV]Sunscreen G
  5. 学会DevOps 能拿多少工资?DevOps 怎么自学?
  6. pandas | 如何在DataFrame中通过索引高效获取数据?
  7. 京东阅读(web)体验优化
  8. socket网络
  9. Django开发之模态框提交内容到后台[Object Object]
  10. PHP min() 函数