Question

205. Isomorphic Strings

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的更多相关文章

  1. [leetcode]205. Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  2. 【刷题-LeetCode】205. Isomorphic Strings

    Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...

  3. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

  4. LeetCode 205. Isomorphic Strings (同构字符串)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  5. 【一天一道LeetCode】#205. Isomorphic Strings

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  6. Baozi Leetcode Solution 205: Isomorphic Strings

    Problem Statement Given two strings s and t, determine if they are isomorphic. Two strings are isomo ...

  7. 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...

  8. Java for LeetCode 205 Isomorphic Strings

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. (easy)LeetCode 205.Isomorphic Strings (*)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

随机推荐

  1. 攻防世界php_rce

    php_rce 进入题目提示为ThinkPHP V5 遇到这种题我们一般去找一下框架的rce漏洞即可,搜索到这样一篇文章 https://www.freebuf.com/articles/web/28 ...

  2. c语言实现两数交换的三种方法

    实现变量的值互相交换的三种不同方法 方法一:利用第三个变量来实现数值的交换 int tmp; tmp = a; a = b; b = tmp; 此方法直观,简易.不易出错,推荐使用 方法二:利用两个变 ...

  3. 前端调试利器 - Charles

    Docs 开发之 Charles 配置指南 1.下载与安装 charles-proxy-4.1.4 .dmg56.12 MB已存到云盘下载 2.激活 使用公司正版license 激活 安装证书 点击证 ...

  4. 【Android开发】【第三方SDK】蒲公英摇一摇

    摇一摇用户信息反馈功能:用户通过摇晃手机或者触发按钮,弹出反馈信息界面,填写个人意见,上传服务器的功能. 1. 蒲公英官网注册应用,获取AppId作为唯一标识: 2. 下载sdk,获取pgyer_sd ...

  5. 获取MCCMNC号

    public static boolean isColombiaSpanishSimCard(){        TelephonyManager telManager = (TelephonyMan ...

  6. number(10,6)正则表达式

    /**     * 判断number(10,6)     * @param dateStr     * @return     */    public boolean isNumJW(String ...

  7. 虚拟机上 安装 CentoOS 7.5 1804 过程记录

    1.准备安装镜像 在开始安装CentOS之前,必须下载安装ISO映像.镜像可从CentOS网站https://www.centos.org/download/.提供以下基本类型的镜像: DVD ISO ...

  8. OllyDbg---数学指令

    数学指令 INC和DEC 分别执行增加1和减少1的操作. ADD 该指令有两个操作数,相加后的结果存放到第一个操作数中. ADDC 带进位的加法 两个操作数的和加上进位标志的值,结果存放到第一个操作数 ...

  9. SMBIOS- DMTF组织指定的规范

    SMBIOS(System Management BIOS , SMBIOS) 是主板或系统制造者以标准格式显示产品管理信息所需遵循的统一规范 SMBIOS是由行业指导机构Desktop Manage ...

  10. hashlib加密模块、logging日志模块

    hashlib模块 加密:将明文数据通过一系列算法变成密文数据 目的: 就是为了数据的安全 基本使用 基本使用 import hashlib # 1.先确定算法类型(md5普遍使用) md5 = ha ...