题目:

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. Android之使用参数改变ProgressDialog的位置、大小、背景透明度、屏幕透明度

    废话不多说,这个改变ProgressDialog的一些配置属性和前面我讲的AlertDialog的设置参数方法一模一样,这里就为了更直观,直接贴实现代码吧: ProgressDialog mProgr ...

  2. dbcp连接池不合理的锁导致连接耗尽

    应用报错,表象来看是连接池爆满了. org.springframework.transaction.CannotCreateTransactionException: Could not open J ...

  3. 高斯函数 --> 高斯分布(正态分布)

    具有如下形式的函数就是高斯函数. 其中a,b,c都是实数常数,a大于0 .由于在博客中写数学公式比较麻烦,还是直接放照片吧. 字写的很难看,不过应该可以看清楚.:(

  4. (NO.00005)iOS实现炸弹人游戏(一):游戏的整体规划设计

    在这新的系列中,我们来尝试完成一款经典的游戏:炸弹人 这是以前红白机上的炸弹人,由于游戏可玩性强,玩法又简单,在后面的机型上陆续推出了很多款续作. 在随后的触屏设备上也出现了炸弹人的模拟版,用的是模拟 ...

  5. Linux:ssh_config快速访问服务器

    在当前用户的根目录下: cd ~/.ssh vi config 编辑config内容为下面: ForwardAgent yes Host 1 Hostname 192.168.1.1 User roo ...

  6. MySQL数据库入门笔记

    2 数据库入门 2.1引入 数据保存到内存: 优点: 1)读写非常快 缺点: 1)程序关闭导致数据丢失 数据保存到文件: 优点: 1)数据可以永久保存 缺点: 1)频繁地IO操作,效率不高! 2)数据 ...

  7. 精通CSS+DIV网页样式与布局--CSS文字效果

    上篇文章,小编简单的介绍了一下CSS的一些基本语法,学习内容不是很复杂,都是CSS的一些入门知识,但是万丈高楼平地起,搭好地基,高楼大厦不在话下,学习任何课程,都必须从基础开始,一步一个脚印,踏实坚定 ...

  8. Android进阶(二)https请求No peer certificate的解决方法.

    在做Android客户端通过https协议访问12306,并爬取数据时,出现了如下错误: 其中有一条错误提示是 javax.net.ssl.SSLPeerUnverifiedException: No ...

  9. Gradle笔记——关于Gradle 1.12

    到目前为止,Gradle已经出到2.1版本了,从1.12这个版本开始看,主要是因为我使用Gradle是Android开发所需要.公司里面是采用Android Studio来进行Android项目的开发 ...

  10. 【一天一道LeetCode】#93. Restore IP Addresses

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