我試圖通過每次它的字母與第二個單詞匹配時重置回圈通過第一個單詞來做到這一點。
function mutation(arr) {
var compare = [];
var firstword = arr[0].toLowerCase();
var secondword = arr[1].toLowerCase();
var j = 0;
for (let i = 0; i < firstword.length; i ) {
if (firstword[i] === secondword[j]) {
compare.push(secondword[i]);
i = -1;
j ;
}
}
let result = compare.join("")
if (result.length === secondword.length) {
return true;
} else {
return false;
}
}
console.log(mutation(["Noel", "Ole"]));
它在某些情況下有效,但在其他情況下,例如上面的示例,則無效。似乎是什么問題?
uj5u.com熱心網友回復:
你需要compare.push(secondword[j])代替compare.push(secondword[i])
顯示代碼片段
function mutation(arr) {
var compare = [];
var firstword = arr[0].toLowerCase();
var secondword = arr[1].toLowerCase();
var j = 0;
for (let i = 0; i < firstword.length; i ) {
if (firstword[i] === secondword[j]) {
compare.push(secondword[j]); // Correction here
i = -1;
j ;
}
}
let result = compare.join("");
if (result.length === secondword.length) {
return true;
} else {
return false;
}
}
console.log(mutation(["Noel", "Ole"]));
此外,您可以考慮使用Array.prototype.every。
const mutation = ([first, sec]) => {
const lowerCaseFirst = first.toLowerCase();
const lowerCaseSec = sec.toLowerCase();
return Array.from(lowerCaseSec).every((ch) => lowerCaseFirst.includes(ch));
};
console.log(mutation(["Noel", "Ole"]));
如果字串很小,則String.prototype.includes可以正常作業,但如果它們很大,則應考慮使用Set。
const mutation = ([first, sec]) => {
const firstSet = new Set(first.toLowerCase());
const lowerCaseSec = sec.toLowerCase();
return Array.from(lowerCaseSec).every((ch) => firstSet.has(ch));
};
console.log(mutation(["Noel", "Ole"]));
uj5u.com熱心網友回復:
簡單的 ES6 函式,我們使用 .every() 檢查 secondword 的每個字符是否包含在 firstword 中。如果確實如此,則回傳 true。
function mutation(arr) {
const firstword = arr[0].toLowerCase();
const secondword = arr[1].toLowerCase();
return secondword.split('').every(char => firstword.includes(char));
}
console.log(mutation(["Noel", "Ole"]));
uj5u.com熱心網友回復:
Set如果您不需要考慮第二個字串中的重復字符,則使用SSM 的答案有效。如果你這樣做了,這里有一個使用Map字符計數的實作。映射鍵是字串 1 中的字符,值是出現次數。例如,如果你想["Noel", "Ole"]回傳true,但["Noel", "Olle"]回傳false(字串 1 不包含 2 個“l”字符)。然后迭代字串 2 并減少字符計數(如果存在)。只要地圖中不存在字符或計數低于 1,函式就會回傳false。
function mutation(arr: string[]): boolean {
return s1ContainsAllCharsInS2(arr[0].toLowerCase(), arr[1].toLowerCase());
}
function s1ContainsAllCharsInS2(s1: string, s2: string): boolean {
if (s2.length > s1.length) {
return false;
}
let charCountMap: Map<string, number> = new Map<string, number>();
Array.from(s1).forEach(c => {
let currentCharCount: number = charCountMap.get(c);
charCountMap.set(c, 1 (currentCharCount ? currentCharCount : 0));
});
return !Array.from(s2).some(c => {
let currentCharCount: number = charCountMap.get(c);
if (!currentCharCount || currentCharCount < 1){
return true;
}
charCountMap.set(c, currentCharCount - 1);
});
}
uj5u.com熱心網友回復:
一種不同的方法。
映射字符并與該地圖進行比較。
function mutation(arr) {
const chars = {};
for (let char of arr[0].toLowerCase()) {
chars[char] = true;
}
for (let char of arr[1].toLowerCase()) {
if (!chars[char]) {
return false;
}
}
return true;
}
console.log(mutation(["Noel", "Ole"]));
console.log(mutation(["Noel", "Oleeeeeeeeeeeee"]));
如果計數也很重要(您的代碼沒有考慮到它),您可以計算每個字符的出現次數并比較這些計數。
function mutation(arr) {
const chars = {};
for (let char of arr[0].toLowerCase()) {
chars[char] = (chars[char] || 0) 1;
}
for (let char of arr[1].toLowerCase()) {
// check if chars[char] contains a (not empty == positive) count
// then decrement it for future checks
if (!chars[char]--) {
return false;
}
}
return true;
}
console.log(mutation(["Noel", "Ole"]));
console.log(mutation(["Noel", "Oleeeeeeeeeeeee"]));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/475103.html
標籤:javascript 数组 循环
