[leetcode]205. Isomorphic Strings同构字符串
哈希表可以用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同构字符串的更多相关文章
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 205 Isomorphic Strings 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...
- LeetCode 205. Isomorphic Strings (同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Leetcode 205 Isomorphic Strings 字符串处理
判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- [LeetCode] 205. Isomorphic Strings 解题思路 - Java
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- 冲刺随笔——Day_Ten
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 团队进行Alpha冲刺 作业正文 正文 其他参考文献 无 ...
- 倾斜摄影实景三维在智慧工厂 Web 3D GIS 数字孪生应用
数字化推动钢铁工业转型升级 数字时代,随着数字地球,数字中国,数字工厂等数字化建设的不断深入,以地理信息系统(Geographic Information System, GIS)为基础,融合大数 ...
- Python中判断字符串是否为数字、字母、标识符、浮点数、大小写、可打印的方法
1.判断s是否都是大写字母:s.isupper(): 2.判断s是否都是小写字母:s.islower(): 3.判断s中的每个单词首字母是否都是大写字母且其他位置无大写字母:s.istitle(),要 ...
- 第15.8节 PyQt入门学习:Designer的界面功能介绍
进入Qt Designer以后,打开或新建一个ui文件,Qt Designer窗口展示的页面如下图所示: 老猿将界面各部分使用红色数字进行标记,按照数字顺序各区域对应功能分别为: 1.界面元素控件区域 ...
- 添加和读取Resources嵌入资源文件(例如.dll和.ssk文件)
前言:有些程序运行的时候,可能调用外部的dll,用户使用时可能会不小心丢失这些dll,导致程序无法正常运行,因此可以考虑将这些dll嵌入到资源中,启动时自动释放.对于托管的dll,我们可以用打包软件合 ...
- 从输入 URL 到页面展示,这中间发生了什么?
当面试官问到,请你说说看"从输入 URL 到页面展示,这中间发生了什么?" 以前的我是这样回答的: 用户输入URL后,向服务器端发起请求.如果顺利,得到网络响应之后,浏览器对资源进 ...
- vue之keep-alive组件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MVC-采用Bundles方式引入css和js文件
优点:修改js或css时会自动生成hash版本号. 缺点:需要在BundleConfig中先添加对应的文件,然后在html中再引用对应的bundle,多操作了一步. web.config中 <c ...
- 黑马2020JAVA-会员版课程
感谢@匿名网友的投稿 投稿人留言:这套教程是我花钱买来的,免费分享供大家参考 最新版的黑马java教程,喜欢编程的朋友可以学习下! 百度网盘: https://pan.baidu.com/s/14AY ...
- linux下为什么每次修改完配置文件之后都需要重新加载配置文件
目录 一.关于inode 二.inode的作用 二.为什么每次修改完服务器配置文件之后,都需要重新加载一下配置文件? 一.关于inode 1.在linux下一切皆文件,linux文件由三部分组成:文件 ...