题目:

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

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given "egg", "add", return true.

Given "foo", "bar", return false.

Given "paper", "title", return true.

Note:
You may assume both s and t have the same length.

思路:

  • 题意:给定两个同样长度的字符串,判断这两个字符串是不是同形,所谓同形就是,两个字符串相等的地方一样,如上面举例的字符串。
  • 可以利用判断字符串是不是有重复字符的思路,把字符串转化为数组,存进hashMap,判断是否有重复值,有了判断相应的坐标是不是相同,不相同,或者不同时出现重复,均为不满足条件。出现重复,去掉重复,添加新元素,不重复,只是添加新元素。

    -

代码:

public class Solution {
    public boolean isIsomorphic(String s, String t) {
        if(s == null){
            return true;
        }
        char[] a = s.toCharArray();
        char[] b = t.toCharArray();
        int n = a.length;
        Map<Character,Integer> aa = new HashMap<Character,Integer>();
        Map<Character,Integer> bb = new HashMap<Character,Integer>();
        for(int i = 0;i < n;i++){
            if(aa.containsKey(a[i])){
                if(!bb.containsKey(b[i])){
                    return false;
                }else{
                    int c = aa.get(a[i]);
                    int d = bb.get(b[i]);
                    if(c != d){
                        return false;
                    }else{
                        aa.remove(a[i]);
                        bb.remove(b[i]);
                        aa.put(a[i],i);
                        bb.put(b[i],i);
                    }
                }
            }else{
                if(bb.containsKey(b[i])){
                    return false;
                }else{
                    aa.put(a[i],i);
                    bb.put(b[i],i);
                }
            }
        }
        return true;
    }
}

LeetCode(44)- Isomorphic Strings的更多相关文章

  1. leetcode:Isomorphic Strings

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

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

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

  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】Isomorphic Strings

    题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  6. Java for LeetCode 205 Isomorphic Strings

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

  7. 【leetcode】Isomorphic Strings(easy)

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

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

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

  9. Java [Leetcode 205]Isomorphic Strings

    题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

随机推荐

  1. 4.QPixmap,QTransform,绘图函数的使用

     新建一个项目Painter MyWidget.h #ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> class MyW ...

  2. SpringMVC实现用户登录实例

    今天分享一下SpringMVC的一个登陆小案例 准备工作 创建一个Dynamic Web Project(本人是Eclipse) 添加相关的jar包,构建路径 创建springMVC-servlet. ...

  3. H5、React Native、Native应用对比分析

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博!iOS开发者交流QQ群: 446310206 "存在即合理".凡是存在的,都是合乎规律的.任何新 ...

  4. android获取设备唯一标示

    概述 有时需要对用户设备进行标识,所以希望能够得到一个稳定可靠并且唯一的识别码.虽然Android系统中提供了这样设备识别码,但是由于Android系统版本.厂商定制系统中的Bug等限制,稳定性和唯一 ...

  5. Swift基础之闭包Closure学习

    首先Swift语言中没有了Block内容,但是你可以通过调用OC文件使用,也可以使用Closure(闭包),实现Block或者Delegae同样反向传值或回调函数的效果,也可以解决函数指针的问题,两者 ...

  6. 【一天一道LeetCode】#237. Delete Node in a Linked List

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

  7. iOS开发之三:常用控件--UILabel的使用

    UILabel 一般用来显示文本内容. 常用的属性如下: @property(nonatomic,copy) NSString *text; // 文本的内容,默认为 nil @property(no ...

  8. android值得珍藏的6个开源框架技术

    1.volley  项目地址 https://github.com/smanikandan14/Volley-demo JSON,图像等的异步下载: 网络请求的排序(scheduling) 网络请求的 ...

  9. Android用AlarmManager实现后台任务-android学习之旅(63)

    因为Timer不能唤醒cpu,所以会在省电的原因下失效,所以需要唤醒cpu在后台稳定化的执行任务,AlarmManager能够唤醒cpu 这个例子讲解了如何通过Service来在后他每一个小时执行.特 ...

  10. iOS中 UIProgressView 技术分享

    UIProgressView 继承自UIView,用来显示进度的,如音乐,视频的缓冲进度,文件的上传下载进度等.让用户知道当前操作完成了多少,离操作结束还有多远 AppDelegate.m Progr ...