205. Isomorphic Strings - LeetCode
Question

Solution
题目大意:判断两个字符串是否具有相同的结构
思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中相同位置字符的差是否相同
Java实现:
public boolean isIsomorphic(String s, String t) {
Map<String, Integer> map = new HashMap<>();
for (int i=0; i<s.length(); i++) {
char c1 = s.charAt(i);
char c2 = t.charAt(i);
String key1 = "s_" + c1;
String key2 = "t_" + c2;
if (map.get(key1) == null) {
map.put(key1, c1 - c2);
} else if (map.get(key1) != c1 - c2) {
return false;
}
if (map.get(key2) == null) {
map.put(key2, c2 - c1);
} else if (map.get(key2) != c2 - c1) {
return false;
}
}
return true;
}
别人的实现:
public boolean isIsomorphic(String s1, String s2) {
int[] m = new int[512];
for (int i = 0; i < s1.length(); i++) {
if (m[s1.charAt(i)] != m[s2.charAt(i)+256]) return false;
m[s1.charAt(i)] = m[s2.charAt(i)+256] = i+1;
}
return true;
}
205. Isomorphic Strings - LeetCode的更多相关文章
- [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
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
- 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 (同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【一天一道LeetCode】#205. Isomorphic Strings
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- Baozi Leetcode Solution 205: Isomorphic Strings
Problem Statement Given two strings s and t, determine if they are isomorphic. Two strings are isomo ...
- 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- (easy)LeetCode 205.Isomorphic Strings (*)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- 180度\360度sg90舵机的使用及代码程序
大部资料都是在网上找到网友大神所共享的,在网上找了几种舵机的,刚接触有点懵,之后找得多了就理解了,想要控制一个硬件就要先了解这个硬件.这里有介绍180度舵机和360度舵机的具体使用,有网上大神的程序, ...
- stm32 中库函数、结构体、地址的强制类型转换、相应特殊功能寄存器之间的关系
以一个挂接在APB2上的外设函数使能为例 A : RCC_APB2PeriphClockCmd():时钟使能函数 1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFI ...
- Anaconda 怎么安装cv2
Anaconda run python程序的时候,如果有import cv2, 但是遇到报错的时候, 可以考虑在anaconda 中安装opencv, 安装过程非常简单. 什么是opencv , op ...
- Centos搭建 Git 服务器教程
搭建 GIT 服务器教程 下载安装 git Git 是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 此实验以 CentOS 7.2 x64 的系统为环境,搭建 git 服 ...
- 项目中常用到的布局 flex
1. 没header,footer固定 html<div class="page"> <div class="top"> <div ...
- Java中的List接口实现类LinkedList
package collection; import java.util.LinkedList; /* * 1.implement List接口 * 2.底层是一个链表结构:查询慢,增删快 * 注意: ...
- 生成swap分区之利用磁盘分区
生成swap 分区方式很多,有利用磁盘分区来生成swap,这种效率比较高,他并不是文件系统, 另外我们还可以拿出磁盘一些空间,做成swap分区还有通过lvm逻辑卷的方式创建swap分区(这种分区就可以 ...
- Grafana中文汉化
可视化图表 Grafana是一个通用的可视化工具.通过Grafana可以管理用户权限,数据分析,查看,导出,设置告警等. 仪表盘Dashboard 通过数据源定义好可视化的数据来源之后,对于用户而言最 ...
- 一行代码的魅力 -- css
<template> <div></div> </template> <script> export default { } </sc ...
- vue 跨域配置代理 get/post 请求
1.第一步要有 axios 插件 : npm i axios 首先要在自己的项目手动添加一个文件 vue.config.js 个人理解的为 这是axios 封装的跨域文件. 2.vue.config. ...