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

时间: 2024-10-27 06:20:43

LeetCode:Isomorphic Strings的相关文章

leetcode笔记:Isomorphic Strings

一.题目描述 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 chara

LeetCode OJ:Isomorphic Strings(同构字符串)

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.

LeetCode之“散列表”:Isomorphic Strings

题目链接 题目要求: 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 c

[LeetCode] 205. Isomorphic Strings 解题思路 - Java

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.

LeetCode 205 Isomorphic Strings

Problem: 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 cha

Java [Leetcode 205]Isomorphic Strings

题目描述: 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 charac

LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

翻译 给定两个字符串s和t,决定它们是否是同构的. 如果s中的元素被替换可以得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换另一个字符串的元素的过程中,所有字符的顺序必须保留. 没有两个字符可以被映射到相同的字符,但字符可以映射到该字符本身. 例如, 给定"egg","add",返回真. 给定"foo","bar",返回假. 给定"paper","title",返回真. 批

leetcode 205. Isomorphic Strings(哈希表)

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.

Java for LeetCode 205 Isomorphic Strings

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.