哈希表可以用ASCII码数组来实现,可以更快

public boolean isIsomorphic(String s, String t) {
/*
思路是记录下每个字符出现的位置,当有重复时,检查另外一个map是不是也是对应位置重复
*/
if (s.length()!=t.length())
{
return false;
} Map<Character,Integer> map1 = new HashMap<>();
Map<Character,Integer> map2 = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c1 = s.charAt(i);
char c2 = t.charAt(i);
//有重复的情况
if (map1.containsKey(c1))
{
//注意这里有个小插曲,就是map得到的包装类,所有包装类比较值 相等要用equals()方法,不能用==,这个字符串是一样的
if ((!map2.containsKey(c2))||(!Objects.equals(map2.get(c2), map1.get(c1))))
{
return false;
}
else
{
map1.put(c1,i);
map2.put(c2,i);
}
}
//没有重复的情况
else
{
if (map2.containsKey(c2))
{
return false;
}
map1.put(c1,i);
map2.put(c2,i);
}
}
return true;
}

[leetcode]205. Isomorphic Strings同构字符串的更多相关文章

  1. [leetcode]205. Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  2. 205 Isomorphic Strings 同构字符串

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

  3. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

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

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

  5. LeetCode 205. Isomorphic Strings (同构字符串)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  6. Leetcode 205 Isomorphic Strings 字符串处理

    判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...

  7. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

  8. [LeetCode] 205. Isomorphic Strings 解题思路 - Java

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. Java for LeetCode 205 Isomorphic Strings

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

随机推荐

  1. Django 在test.py 中测试文件的配置

    import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTING ...

  2. windows中flask的环境搭建

    之前在ctf中遇到了python模板注入的题,于是就打算学习一下flask框架,它是基于Python的Web轻量级应用框架,与其他框架相比,Flask可以自主选择应用组件,可扩展性强. 安装也简单 第 ...

  3. Docker 与 Podman 容器管理的比较

    翻译自 Paul Ferrill 2020年9月1日的文章<Compare Docker vs. Podman for container management> [1] Docker 和 ...

  4. 第3.10节 Python强大的字符串格式化新功能:使用format字符串格式化

    一.    引言 前面两节介绍的字符串格式化方法,都有其本身对应的缺陷,老猿不建议大家使用,之所以详细介绍主要是考虑历史代码的兼容性,方便大家理解前人留下的代码.老猿推荐大家新编码时使用format方 ...

  5. 第四十一章、PyQt显示部件:TextBrowser、CalendarWidget、LCDNumber、ProgressBar、Label、HorizontalLine和VerticalLine简介

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 在Designer中,显示部件有Labe ...

  6. PyQt(Python+Qt)学习随笔:QColumnView的resizeGripsVisible属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QColumnView在一个视图中展示多个列表,每个下级列表是上一个列表的数据项的分支, QColu ...

  7. PyQt(Python+Qt)学习随笔:QTreeView树形视图的indentation属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的indentation属性用于控制视图中每级数据项之间的缩进,对于顶级项 ...

  8. WindowsServerU盘系统盘制作

    一.工具及安装包准备: 1.UltraISO软碟通 下载:链接:https://pan.baidu.com/s/1gixSdpEjvh6I31rGeh1-Gg 提取码:9zbx (大学期间无意间找到一 ...

  9. vue 中 this.$options.data() 重置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. php项目从github自动pull到服务器

    php项目github自动pull到服务器 项目名:web 一.自动触发 1.在服务器添加脚本文件:gitpull.sh #!/bin/sh cd /www/web git reset --hard ...