题目原文:
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..
题目大意:
给出字符串s和t,判断他们是不是同构(isomorphic)的。
同构的定义是s和t具有相同的结构,即可用t串一个字符代替s串的一个字符,而其他字符不变。
例如,”egg”和 ”add”是同构的;”foo”和”bar”不是同构的;”paper”和”title ”是同构的。
保证s串和t串的长度相等。
题目分析:
使用一个HashMap记录一组key-value对,key为s串中每个字符,value为t串中每个字符。如果出现相同的key则不是同构的。
例如s=”foo”,t=”bar”,则存在o-b和o-r两个同key的映射。
但这样还不够,还要再按相同方法建立t串到s串的map,因为若s=”bar”,t=”foo”则不存在s串到t串中的相同key,但存在t串到s串的相同key。
源码:(language:java)

public class Solution {public boolean isIsomorphic(String s, String t) {int slen=s.length();int tlen=t.length();if(slen!=tlen)return false;else{HashMap<Character,Character> map=new HashMap();for(int i=0;i<slen;i++){if(!map.containsKey(s.charAt(i)))map.put(s.charAt(i),t.charAt(i));else{if(map.get(s.charAt(i)) != t.charAt(i))return false;}}map.clear();for(int i=0;i<slen;i++){if(!map.containsKey(t.charAt(i)))map.put(t.charAt(i),s.charAt(i));else{if(map.get(t.charAt(i)) != s.charAt(i))return false;}}return true;            }}
}

成绩:
34ms,beats 30.09%,众数26ms,8.93%

Easy-题目46:205. Isomorphic Strings相关推荐

  1. 205 Isomorphic Strings

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

  2. 205. Isomorphic Strings

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

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

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

  4. leetcode python3 简单题205. Isomorphic Strings

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

  5. leetcode之Isomorphic strings

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

  6. hdu 6863 Isomorphic Strings

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

  7. [LeetCode] Isomorphic Strings - 字符串操作:数组计数字符个数问题

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

  8. CodeForces - 985F Isomorphic Strings

    假如两个区间的26的字母出现的位置集合分别是 A1,B1,A2,B2,....., 我们再能找到一个排列p[] 使得 A[i] = B[p[i]] ,那么就可以成功映射了. 显然集合可以直接hash, ...

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

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

最新文章

  1. oracle直查和call哪个更快,让oracle跑的更快1读书笔记二
  2. 春节囤货清单 | 15篇近期值得读的AI论文
  3. 【loj6342】跳一跳 期望dp
  4. 类型转换:隐式转化(算数转换,整型提升,混合提升,赋值转换),强制转换【C语言】
  5. TCP通信的服务器端代码实现
  6. 【图像超分辨率】Remote Sensing Image Super-resolution: Challenges and Approaches
  7. 《天天数学》连载07:一月七日
  8. STM32网络之中断
  9. 深入研究memcache 特性和限制
  10. 小括号教学设计导入_如何在教学中凸显出学生的主体地位呢?原来只需要做到这几点即可...
  11. Python实战从入门到精通第六讲——数据结构与算法4之过滤序列元素
  12. Java 读取文件大写和格式化输出字串
  13. [HTML5和Flash视频播放器]Video.js 学习笔记(一 ) HLS库:videojs-contrib-hls
  14. 鸿蒙支持ps4手柄吗,完美兼容PS4手柄!iPhone也能畅玩PS4,教程在此
  15. 虚拟机恢复初始化设置
  16. SAP_FI统驭科目
  17. 租用服务器如何选择带宽,带宽越大越好吗
  18. BZOJ 1812 IOI 2005 riv
  19. 通信原理中星座图详解
  20. android 常用机型尺寸_Android设备尺寸

热门文章

  1. Tensorflow实现二次元图片的超分辨率
  2. 算丰边缘计算开发板人脸检测识别-实现原理与代码介绍
  3. CSS3动画实现小熊奔跑
  4. 自定义view(二)
  5. python 在线学习站点
  6. Python深度学习篇六《深度学习用于文本和序列》
  7. VUE的proxyTable使用记录和java跨域设置
  8. Docker-提交(Commit)自己的一个镜像
  9. 功能设计:给淘宝设计一个多人拼单购买的新功能
  10. 【木头Cocos2d-x 028】Lua篇(第03章):简单解析Lua的堆栈