题目描述:

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.

要完成的函数:

bool isIsomorphic(string s, string t)

说明:

1、如果两个相同长度的字符串s和t,s中相同的字母能由t中相同位置上的同一个字母来代替,那么它是同构的。比如“add”和“egg”,a由相同位置上的e代替,d由相同位置上的g代替。也就是说要建立对应关系。

2、不能两个不同的字母对应同一个字母,比如“ab”和“ee”就不是同型的,“ee”和“ab”也不是同型的。

3、但是“paper”和“title”是同型的,因为可以e对应l,r对应e。

根据上述规则,我们利用map可以构建出如下代码,一旦出现两个不同的字母试图对应同一个字母,就可以判断它是不同型的。

bool isIsomorphic(string s, string t)
{map<char,char>m1;map<char,char>m2;for(int i=0;i<s.size();i++)//去除"foo"和“bar”这种同一个字母对应两个的情况{if(!m1[s[i]])m1[s[i]]=t[i];else{if(m1[s[i]]!=t[i])return false;}}for(int i=0;i<t.size();i++)//去除“ab”和“ee”这种两个字母对应同一个的情况{if(!m2[t[i]])m2[t[i]]=s[i];else{if(m2[t[i]]!=s[i])return false;}}return true;
}

上述代码实测12ms,beats 51.61% 0f cpp submissions。

4、改进

在discuss区看到了这样的代码,也不难理解。

bool isIsomorphic(string s, string t)
{char map_s[128] = { 0 };char map_t[128] = { 0 };int len = s.size();for (int i = 0; i < len; ++i){if (map_s[s[i]]!=map_t[t[i]]) return false;map_s[s[i]] = i+1;map_t[t[i]] = i+1;}return true;
}

实测9ms,beats 71.54% of cpp submissions。

还有一大堆人做出来7ms的方法,很好奇……

转载于:https://www.cnblogs.com/chenjx85/p/8822259.html

leetcode-205-Isomorphic Strings相关推荐

  1. LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

    翻译 给定两个字符串s和t,决定它们是否是同构的.假设s中的元素被替换能够得到t,那么称这两个字符串是同构的.在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两个字 ...

  2. 205 Isomorphic Strings

    205 Isomorphic Strings 链接:https://leetcode.com/problems/isomorphic-strings/ 问题描述: Given two strings ...

  3. leetcode之Isomorphic strings

    问题来源:Isomorphic strings 问题描述:给定两个字符串,判断它俩是否满足同构关系.同构关系表示两个字符串的字符能一一对应,例如title和paper构成同构关系,而foo和bar不构 ...

  4. leetcode python3 简单题205. Isomorphic Strings

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零五题 (1)题目 英文: Given two strings s and t ...

  5. 205. Isomorphic Strings

    1,题目要求 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if th ...

  6. Easy-题目46:205. Isomorphic Strings

    题目原文: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  7. 【大数相乘】LeetCode 43. Multiply Strings

    LeetCode 43. Multiply Strings Solution1:我的答案 作为一个hard题,提交一次就过真是让我hin开心啊!!! 就是方法有点笨.. class Solution ...

  8. hdu 6863 Isomorphic Strings

    Isomorphic Strings 题解:暴力枚举n的所有因子,判断是否合法.判断合法第一步:判断每段中各字符出现次数是否相同,第二部:判断每段的最小表示是否相同.因子还可以减枝. #include ...

  9. leetcode 205.同构字符串

    leetcode 205.同构字符串 题目描述 给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符 ...

  10. C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...

最新文章

  1. 最好用的markdown编辑器推荐typora
  2. 《Programming WPF》翻译 第3章 2.处理输入
  3. 三维点云数据处理软件供技术原理说明_海量点云数据处理理论与技术
  4. Entity Framework 普通操作(复习用)——感觉有点不对,需要撸代码验证
  5. c语言实现二分法_C语言实现二分法求解方程在区间内的根
  6. 微软职位内部推荐-Sr. SW Engineer for Azure Networking
  7. jsp session
  8. 我知道关于马云的故事,他不成功都难
  9. React:引入echarts绘制图表
  10. 再见2018,你好2019
  11. 解决windows下的mysql匿名登陆无法使用mysql数据库的问题
  12. 美食网页设计作品html,美食网页设计与制作.doc
  13. 更适合小孩的电动牙刷,轻柔呵护口腔,Combo咸蛋超人电动牙刷上手
  14. Occlusion Culling(遮挡剔除)
  15. NVIDIA Jetson TK1 rosrun rviz segmentation fault的解决方法
  16. Kafka偏移量(Offset)管理
  17. 从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序
  18. java validate 框架_使用validate验证框架
  19. LED流水灯,从左到右的实现流程
  20. 求最小公倍数(C++牛客网)

热门文章

  1. Html中代码换行造成空格间距的问题
  2. CABasicAnimation animationWithKeyPath Types
  3. Labview实现字符串加密
  4. [RN] React Native 解决 使用 阿里巴巴 矢量图标库 iconfont 图标不垂直居中问题
  5. .html() 与.text() 获取值、取值 区别
  6. Spring系列:学习Spring的资源和讨论
  7. 使用树莓派 Raspberry Pi 播放豆瓣 FM
  8. C++ 入门2 ---- 类型转换
  9. Objective-C GCD深入理解
  10. Codeforces Round #503 (by SIS, Div. 2)