leetCode刷题记录32_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. No two characters may map to the same character but a character may map to itself.
Example 1:
    Input: s = "egg", t = "add"
    Output: true
Example 2:
    Input: s = "foo", t = "bar"
    Output: false
Example 3:
    Input: s = "paper", t = "title"
    Output: true
Note:
You may assume both s and t have the same length.
给定两个字符串,返回第二个字符串可否通过替换操作变成第一个字符串
/*****************************************************我的解答*************************************************
/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isIsomorphic = function(s, t) {
    var replaceCount = 0;
    if(s.length != t.length)
    {
        return false;
    }    
    /* for(var index = 0; index < s.length; index++)
    {
        s = s.replace(new RegExp(s.charAt(index),'g'),t.charAt(index));
        replaceCount += s.match(new RegExp(t.charAt(index),'g')).length;
        if(replaceCount == s.length)
        {
            break;
        }    
    }    
    return s == t; */
};
console.log(isIsomorphic("qwertyuiop[]asdfghjkl;'\\zxcvbnm,./", "',.pyfgcrl/=aoeuidhtns-\\;qjkxbmwvz"));

猜你喜欢

转载自blog.csdn.net/gunsmoke/article/details/87938917
今日推荐