Created
March 1, 2023 19:50
-
-
Save abel-masila/a0eeba3122c0a47124811410351dbdf5 to your computer and use it in GitHub Desktop.
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
function makePalindrome(s, startindex, endindex, subs) { | |
let result = ""; | |
for (let i = 0; i < startindex.length; i++) { | |
let substring = s.substring(startindex[i], endindex[i] + 1); | |
let count = 0; | |
let charMap = new Map(); | |
for (let j = 0; j < substring.length; j++) { | |
charMap.set(substring.charAt(j), charMap.get(substring.charAt(j)) + 1 || 1); | |
} | |
for (let [char, freq] of charMap) { | |
if (freq % 2 === 1) { | |
count++; | |
} | |
} | |
if (count > subs[i]) { | |
result += "0"; | |
} else { | |
result += "1"; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function palindromeChecker(s, startindex, endindex, subs) {
let result = '';
for (let i = 0; i < startindex.length; i++) {
let sub = s.substring(startindex[i], endindex[i] + 1);
let count = new Array(26).fill(0);
for (let j = 0; j < sub.length; j++) {
count[sub.charCodeAt(j) - 97]++;
}
let oddCount = 0;
for (let j = 0; j < 26; j++) {
if (count[j] % 2 !== 0) {
oddCount++;
}
}
if (oddCount <= subs[i] * 2 + 1) {
result += '1';
} else {
result += '0';
}
}
return result;
}