Modify the String prototype to add a new method isAnagram
.
isAnagram
takes a single string argument. It returns true if that string
is an anagram of the string it was called on, and false otherwise.
Example:
("roasting").isAnagram("organist"); // true
("presence").isAnagram("presents"); // false
Anagrams should ignore spaces, punctuation, and capitalization:
("Quid est veritas?").isAnagram("Est vir qui adest."); // true
큰 흐름은 이렇다.
공백과 특수문자를 제외한 문자만 추출
문자열의 구성성분이 동일한 지 확인
String.prototype.isAnagram = function (string) {
const reg = /[ \{\}\[\]\/?.,;:|\)*~`!^\-_+┼<>@\#$%&\ '\"\\(\=]/gi;
let [str1, str2] = [this.replace(reg, ""), string.replace(reg, "")];
if (str1.length !== str2.length) {
return false;
}else {
str1 = Array.from(str1.toUpperCase()).sort();
str2 = Array.from(str2.toUpperCase()).sort();
return str1.toString() === str2.toString();
};
}
String.prototype.isAnagram = function (string) {
let [str1, str2] = [Array.from(this.toUpperCase()), Array.from(string.toUpperCase())];
function standardizeStr (str) {
const _str = Array.from(str.toUpperCase());
const result = _str.fillter(char => char.charCodeAt() >= 65 && char.charCodeAt() <= 90);
return result;
}
if (str1.length !== str2.length) {
return false;
}else {
str1 = Array.from(str1.toUpperCase()).sort();
str2 = Array.from(str2.toUpperCase()).sort();
return str1.toString() === str2.toString();
};
}