LeetCode全集请参考:LeetCode Github 大全

题目

205. Isomorphic Strings

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.

Example 1:

Input: s = "egg", t = "add"
Output: true

Example 2:

Input: s = "foo", t = "bar"
Output: false

Example 3:

Input: s = "paper", t = "title"
Output: true

Note:
You may assume both s and t have the same length.

解法

用双map记录单词出现的位置,如果出现过并且位置相同,则继续;否则就失败。
这里需要特别注意,遍历的时候要用Integer,因为即使相同的int装箱后也是不同的Integer对象。

package hashtable;// https://leetcode.com/problems/isomorphic-strings/import java.util.HashMap;
import java.util.Map;public class IsomorphicStrings {public boolean isIsomorphic(String s, String t) {// check edgeif (s == null || t == null) {return s == t;}int len = s.length();if (len != t.length()) {return false;}Map<Character, Integer> smap = new HashMap<>();Map<Character, Integer> tmap = new HashMap<>();for (Integer i = 0; i < len; i++) {char sc = s.charAt(i);char tc = t.charAt(i);addItem(smap, i, sc);addItem(tmap, i, tc);if (smap.get(sc) != tmap.get(tc)) {return false;}}return true;}private void addItem(Map<Character, Integer> map, Integer i, char c) {if (!map.containsKey(c)) {map.put(c, i);}}
}

算法:同构字符串205. Isomorphic Strings相关推荐

  1. 同构字符串(Isomorphic Strings)

    一.学习要点: 1.构造两个哈希表: 2.由于为字符串,每个哈希表的长度为256: 3.字符串的字符比较则为哈希值的比较:如果出现之前构造过哈希值的字符,则取出哈希值比较即可,同一映射的哈希值相同:不 ...

  2. 205 Isomorphic Strings

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

  3. leetcode python3 简单题205. Isomorphic Strings

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

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

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

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

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

  6. 205. Isomorphic Strings

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

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

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

  8. 算法----------同构字符串(Java版本)

    题目 给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同 ...

  9. leetcode 205.同构字符串

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

  10. CodeForces - 985F Isomorphic Strings(字符串哈希)

    题目链接:点击查看 题目大意:首先规定同构字符串,若字符串s和字符串t互为同构字符串,则必须满足: 两个字符串长度相同 s中的字符种类数与t中的字符种类数相同 s中的每一个字母在t中都有对应,且必须是 ...

最新文章

  1. 一个39岁程序员的应聘被拒
  2. ngx_lua与go高并发性能对比
  3. docker之docker-machine用法
  4. 关于WCF中间层服务器端DTO属性更新如何同步回仓储实体的处理方式
  5. 《统计学》学习笔记之导论
  6. 23V3有这种C语言表达式吗,数据结构(C语言版第2版_李云清)习题答案2012-12.doc
  7. Spring Data JPA的持久层
  8. [精品]CSAPP Bomb Lab 解题报告(六)
  9. PHP中unset,array_splice删除数组中元素的区别
  10. alot英文怎么读_很多的英文怎么说
  11. python安装要什么配置_Python的安装和详细配置
  12. shareSDK 提示#warning:尚未配置[新浪微博]URL Scheme:sinaweibosso.或wb
  13. php.ini 没有pdo,php.ini 没有pdo怎么办
  14. Linux操作环境下配置MMIX环境
  15. 360服务器安全加固系统,360政企安全
  16. Windows下彻底卸载MySQL数据库
  17. linux端口快速释放,Linux 快速释放端口与释放内存缓存,linux释放端口缓存
  18. 分享一个挺不错的Git视频教程
  19. kotlin一点摸索
  20. 【深度学习】cs231n计算机视觉 SVM分类器

热门文章

  1. java 反序列化 怎么知道类_理解java中的序列化与反序列化
  2. Scrapy保存中文字符到json文件时编码设置
  3. WordPress 插件机制的简单用法和原理(Hook 钩子)
  4. 使用ASP.NET Atlas ItemView控件显示集合中的单个数据
  5. 动态规划——Palindrome Partitioning II
  6. VC++ chap19 动态链接库 VC++ 孙鑫
  7. 进展-Silverlight5、 windows phone 7、pc 三栖引擎,2D核心已完成
  8. Visual Studio 2010 Beta版包括InstallShield Limited Edition
  9. 无类IP地址与子网的算法
  10. 智能实验室-杀马(Defendio) 4.11.0.790