Last active
August 29, 2015 14:05
-
-
Save crimx/07b662a53f4c60174a8a to your computer and use it in GitHub Desktop.
给定一长一短的两个字符串 A,B,假设 A 长 B 短,要判断 A 是否包含了 B 中所有的字母。《程序员编程艺术二》
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.myApp = (function (myApp) { | |
// O(m+n) | |
myApp.containChar = function (Str1, Str2) { | |
if (typeof Str1 !== "string" || typeof Str2 !== "string") { | |
return; | |
} | |
var longStr, | |
shortStr, | |
alphabet = {}, // 统计 shortStr 字母表 | |
alphaLen = 0, // shortStr 字母类型个数 | |
count = 0, // 匹配的字母的类型个数 | |
i, t; | |
if (Str1.length > Str2.length) { | |
longStr = Str1; | |
shortStr = Str2; | |
} else { | |
longStr = Str2; | |
shortStr = Str1; | |
} | |
for (i = 0; i < shortStr.length; i += 1) { | |
t = shortStr.charAt(i); | |
if (!alphabet.hasOwnProperty(t)) { | |
alphabet[t] = true; | |
alphaLen += 1; | |
} | |
} | |
for (i = 0; i < longStr.length; i += 1) { | |
t = longStr.charAt(i); | |
if (alphabet.hasOwnProperty(t)) { | |
if (alphabet[t] === true) { | |
alphabet[t] = false; | |
count += 1; | |
// 把 shortStr 字母表所有类型的字母都匹配了,不用继续匹配 | |
if (count === alphaLen) { | |
return true; | |
} | |
} | |
} else { | |
return false; | |
} | |
} | |
return true; | |
}; // end function containChar | |
return myApp; | |
}(window.myApp || {})); | |
console.log(myApp.containChar("abcdefghijk", "abc")); // true | |
console.log(myApp.containChar("abc", "abcdefghijk")); // true | |
console.log(myApp.containChar("abcdefghijk", "zabc")); // false | |
console.log(myApp.containChar("abcdefghijk", "")); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment