Skip to content

Instantly share code, notes, and snippets.

@abel-masila
Created March 1, 2023 19:50
Show Gist options
  • Save abel-masila/a0eeba3122c0a47124811410351dbdf5 to your computer and use it in GitHub Desktop.
Save abel-masila/a0eeba3122c0a47124811410351dbdf5 to your computer and use it in GitHub Desktop.
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;
}
@abel-masila
Copy link
Author

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment