LeetCode:Isomorphic Strings - 同构的字符串

1、题目名称

Isomorphic Strings(同构的字符串)

2、题目地址

https://leetcode.com/problems/isomorphic-strings/

3、题目内容

英文:

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.

中文:

给出两个字符串s和t,确定它们是否同构。两个字符串是同构的充要条件是它们之间相同位置的字符存在一一对应关系可以相互替换。在字符串s中不存在两个不同的字符映射到t中同一个字符的情况,但一个字符可以映射到它自身。

例如:egg和add是同构的,foo和bar不是同构的,paper和title是同构的

4、解题方法1

如果使用Java语言,使用HashMap来记住这些字符对是一个很方便的做法。在这里面我用了两个HashMap,一个HashMap用来记s的字符到t的映射,用于判断s中的两个字符不能用t中的同一个字符替换,另一个HashMap用来记t的字符到s的映射,用于判断t中的两个字符不能由s中同一个字符映射而来。实现的Java代码如下:

import java.util.HashMap;

/**
 * 功能说明:LeetCode 205 - Isomorphic Strings
 * 开发人员:Tsybius2014
 * 开发时间:2015年8月8日
 */
public class Solution {
    
    /**
     * 判断字符串是否同构
     * @param s 字符串s
     * @param t 字符串t
     * @return
     */
    public boolean isIsomorphic(String s, String t) {
        
        if (s.length() != t.length()) {
            return false;
        }

        HashMap<Character, Character> hashMapS = new HashMap<Character, Character>();
        HashMap<Character, Character> hashMapT = new HashMap<Character, Character>();
        
        for (int i = 0; i < s.length(); i++) {
            if (hashMapS.containsKey(s.charAt(i))) {
                if (hashMapS.get(s.charAt(i)) != t.charAt(i)) {
                    return false;
                }
            } else {
                if (hashMapT.containsKey(t.charAt(i))) {
                    return false;
                }
                hashMapS.put(s.charAt(i), t.charAt(i));
                hashMapT.put(t.charAt(i), s.charAt(i));
            }
        }
        
        return true;
    }
}

5、解题方法2

另一个不使用HashMap的方法是使用两个数组,其思路和HashMap类似,实现的Java代码如下:

/**
 * 功能说明:LeetCode 205 - Isomorphic Strings
 * 开发人员:Tsybius2014
 * 开发时间:2015年8月8日
 */
public class Solution {
    
    /**
     * 判断字符串是否同构
     * @param s 字符串s
     * @param t 字符串t
     * @return
     */
    public boolean isIsomorphic(String s, String t) {
        
        if (s.length() != t.length()) {
            return false;
        }

        //虽然不考虑英文大小写,但因为不仅仅有26个英文字母,还可能有其他符号,所以数组要开得足够大
        int len = 40;  //这里数组长度开到40,可以满足题目AC
        char[] charInS = new char[len];
        char[] charInT = new char[len];
        for (int i = 0; i < len; i++) {
            charInS[i] = '0';
            charInT[i] = '0';
        }
        
        boolean bTrue;
        for (int i = 0; i < s.length(); i++) {
            bTrue = false;
            //在数组s中找到当前字符串s的字母,如果数组t中相同位置字母不一致,则字符串不同构
            for (int j = 0; j < len; j++) {
                if (charInS[j] == '0') {
                    break;
                } else if (charInS[j] == s.charAt(i)) {
                    if (charInT[j] != t.charAt(i)) {
                        return false;
                    }
                    bTrue = true;
                } 
            }
            if (!bTrue) {
                //在数组t中找到当前字符串t的字母,如果数组s中相同位置字母不一致,则字符串不同构
                for (int j = 0; j < len; j++) {
                    if (charInT[j] == '0') {
                        break;
                    } else if (charInT[j] == t.charAt(i)) {
                        return false;
                    } 
                }
                //记录新的组合
                for (int k = 0; k < len; k++) {
                    if (charInS[k] == '0') {
                        charInS[k] = s.charAt(k);
                        charInT[k] = t.charAt(k);
                        break;
                    }
                }
            }
        }
        
        return true;
    }
}

END

转载于:https://my.oschina.net/Tsybius2014/blog/489587

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值