leetcode.字符串.205同构字符串-Java
1. 具体题目
给定两个字符串 s 和 t,判断它们是否是同构的。如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
示例 1: 输入: s = "egg", t = "add" 输出: true
示例 2: 输入: s = "foo", t = "bar" 输出: false
示例 3: 输入: s = "paper", t = "title" 输出: true
2. 思路分析
利用哈希表的映射关系,将两个字符串中各字符的对应关系存入表中,之后查找这个表做判断。
3. 代码
public boolean isIsomorphic(String s, String t) {
//可对比 242.有效的字母异位词
if(s.length() != t.length()) return false;
HashMap<Character, Character> mapping = new HashMap<>();
for(int i = 0; i < s.length(); i++){
if(mapping.containsKey(s.charAt(i))){
if(mapping.get(s.charAt(i)) != t.charAt(i)) return false;
}else{
if(mapping.containsValue(t.charAt(i))) return false;
mapping.put(s.charAt(i), t.charAt(i));
}
}
return true;
}
4. 思路优化
不用构建各字符一一对应的关系,只要判断对应字符出现的位置相同即可,也就是两个字符串中相同位置的字符,它们在各自字符串中每次出现的位置都应相同。为了方便,每次比较两字符首次出现的位置。
5. 代码优化
//参考别人答案,利用String类方法 indexOf(char c),返回指定字符在字符串中第一次出现的位置
public boolean isIsomorphic(String s, String t) {
char[] ch1 = s.toCharArray();
char[] ch2 = t.toCharArray();
int len = s.length();
for (int i = 0; i < len; i++) {
if(s.indexOf(ch1[i]) != t.indexOf(ch2[i])){
return false;
}
}
return true;
}
leetcode.字符串.205同构字符串-Java的更多相关文章
- Java实现 LeetCode 205 同构字符串
205. 同构字符串 给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序. ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- leetcode刷题记录——字符串
242.有效地字母异位词 由于本题的字符串只包含 26 个小写字符,因此可以使用长度为 26 的整型数组对字符串出现的字符进行统计,并对比字母出现的次数是否一致.不再使用 HashMap. toCha ...
- LeetCode 205:同构字符串 Isomorphic Strings
题目: 给定两个字符串 s 和 *t*,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 *t* ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字 ...
- 205 Isomorphic Strings 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...
- 【leetcode 简单】 第五十九题 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一 ...
- LeetCode OJ:Isomorphic Strings(同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [Swift]LeetCode205. 同构字符串 | Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- 1. 什么是Prometheus
什么是Prometheus Prometheus是一个最初在SoundCloud上构建的开源系统监视和警报工具包 .自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有一个非常 ...
- Django框架(十六)—— cookie和session组件
目录 cookie和session组件 一.cookie 1.cookie的由来 2.什么是cookie 3.cookie的原理 4.cookie的覆盖 5.在浏览器中查看cookie 6.cooki ...
- Java拦截过滤器模式
当我们想要对应用程序的请求或响应进行一些预处理/后处理时,使用截取过滤器设计模式. 在将请求传递到实际目标应用程序之前,在请求上定义和应用过滤器. 过滤器可以进行请求的认证/授权/日志记录或跟踪,然后 ...
- fedora 25重新安装引导
引导区被其他系统给覆盖了,重新安装引导 grub2-install /dev/sdb GRUB_SAVEDEFAULT=true BIOS grub2-mkconfig -o /boot/grub2/ ...
- Spring Cloud配置中心客户端读取配置
微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies> <dependency> <groupId>org.springframework ...
- C# winform 动态操作webService
1.动态操作webService类 public static class WebServiceHelper { #region InvokeWebService //动态调用web服务 public ...
- C语言printf函数
#include<stdio.h> //int float double short char long int main() { //int printf(const char *for ...
- Activity向Fragment传值
发送数据 //Activity传值,通过Bundle Bundle bundle = new Bundle(); bundle.putString("MainActivity", ...
- Form与ModelForm中的插件使用
一.Form插件的使用 (一)widget参数 from .models import * from django import forms from django.forms import widg ...
- Python删除文件夹
import os os.rmdir('OS-Demo-2') os.removedirs('OS-Demo-3/sub-Dir-1') os.removedirs()会自动将上一级文件夹也删除,谨慎 ...