[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 ...
随机推荐
- Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?
Python中使用百分号占位符的字符串格式化方法中%s和%r表示需要显示的数据对应变量x会以str(x)还是repr(x)输出内容展示. 关于str和repr的关系请见: <Python中rep ...
- 转:http协议学习系列(响应头---Response Headers)
HTTP最常见的响应头如下所示: ·Allow:服务器支持哪些请求方法(如GET.POST等): ·Content-Encoding:文档的编码(Encode)方法.只有在解码之后才可以得到Conte ...
- Python(Python+Qt)学习随笔:使用xlwings新建Execl文件和sheet的方法
在<Python学习随笔:使用xlwings读取和操作Execl文件>介绍了使用xlwings读取和操作Execl文件的方法,但老猿这两天写个例子使用时,发现使用该文的方法无法新建EXCE ...
- [BJDCTF2020]Mark loves cat && [BJDCTF 2nd]简单注入 && [BJDCTF2020]The mystery of ip
[BJDCTF2020]Mark loves cat 源码泄露 使用GitHack.py下载源码 下载之后对源代码进行审计 flag.php代码为: <?php $flag = file_get ...
- python 读取excel表格内不同类型的数据
不同类型数据对应值: #coding=utf-8 import os import xlrd from datetime import datetime,date newparh = os.chdir ...
- 沪苏浙皖共同打造区块链数字经济发展高地,Panda Global表示区块链真的来了!
近日,在长三角一体化发展重大合作事项签约仪式上,沪苏浙皖经信部门共同签约,推进长三角区块链数字经济一体化发展,共同打造数字经济发展高地.从此次签约活动也能看出来,区块链数字现金的发展已经得到了认可,早 ...
- 题解-Roman and Numbers
题解-Roman and Numbers 前置知识: 数位 \(\texttt{dp}\) </> \(\color{#9933cc}{\texttt{Roman and Numbers} ...
- 使用plesk遇到的问题
按照plesk使用指南中,"快速建站"的部分,配置一番后,还是访问不了网站. 后来解决了,原因如下: 主域名没有解析,只解析了,带www的子域名 80端口没开
- 一文入门Redis
一文入门Redis 目录 一文入门Redis 一.Redis简介 二.常用数据类型 1.String(字符串) 2.Hash(哈希) 3.List(列表) 4.Set(集合) 5.Zset(有序集合) ...
- 如何配置nginx达到只允许域名访问网址,禁止ip访问的效果
需求:接入阿里云的waf对网站进行防护,但是如果直接通过IP地址访问网站即可绕过阿里云waf,于是希望禁止通过ip访问网站 修改nginx配置文件 在server段里插入如下内容即可 if ($hos ...