哈希表可以用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. MySQL 当前时间,今日时间,前日时间 详解

    MySQL 获取当前日期及日期格式 获取系统日期: NOW() 格式化日期: DATE_FORMAT(date, format) 注: date:时间字段 format:日期格式 返回系统日期,输出 ...

  2. 教学之Treap

    放在前面的话 本蒟蒻因为最近的题目总是搞点奇奇怪怪的平衡树,就去学了下\(Treap\) 现在来总结一下 由于本人是个蒟蒻,本文可能有部分错误,麻烦各位读者大佬在评论区提醒 什么是\(Treap\) ...

  3. java ipv6发邮件需要注意的点

    和ipv4发邮件一样,毕竟ip只是用来找地址的,v4 v6使用上基本没区别. 但有一点得注意:java ipv6采用发送RST包来通知邮件服务器断开连接,这样会导致客户端抛 MessagingExce ...

  4. 学习tcp和udp

    tcp和udp的差别 tcp和udp是不一样的 tcp是一个可靠的连接,如果发出一个tcp请求,那么就一定要回复一个请求 而udp是不需要回复的,就像QQ一样,只要发过来就可以了,不管别人在不在线,发 ...

  5. STL—— 容器(vector)元素的删除

    1. clear() 将整个 vector 都删除 使用 vectorname.clear() 可以将整个vector 中的元素全部删除,但是内存不会释放,如下代码: 1 #include <i ...

  6. mysql 5.7全备脚本

    #!/bin/bash mysql_user="root" mysql_password="mysqlpassword" mysql_host="19 ...

  7. SpringBoot基于EasyExcel解析Excel实现文件导出导入、读取写入

    1. 简介   Java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题 ...

  8. Jmeter +Maven+jenkins+eclipse 接口自动化测试

    背景: 首先用jmeter录制或者书写性能测试的脚本,用maven添加相关依赖,把性能测试的代码提交到github,在jenkins配置git下载性能测试的代码,配置运行脚本和测试报告,配置运行失败自 ...

  9. AWT03-LayoutManager布局管理器

    1.LayoutManager布局管理器 在之前的学习中,我们使用setBounds()方法设置容器的位置大小,但我们不得不明白一件事--如果我们手动为容器设置位置大小的话,就会造成程序通用性的下降. ...

  10. 1款开源工具,实现自动化升级K3S集群!

    即便你的集群能够平稳运行,Kubernetes升级依旧是一项艰难的任务.由于每3个月Kubernetes会发布一个新版本,所以升级是十分必要的.如果一年内你不升级你的Kubernetes集群,你就会落 ...