You are viewing a single comment's thread. Return to all comments →
JavaScript Solution
let count = 0 let str = s.split('') for (let i = 0, j = n - 1; i <= j; i++, j--) { if (str[i] !== str[j]) count++ } if (count > k) return -1 for (let i = 0, j = n - 1; i <= j; i++, j--) { if (i === j) { if (k > 0) str[i] = '9'; break; } let available = (k - (str[i] !== '9') - (str[j] !== '9')) let needed = (count - (str[i] !== str[j])) if (available >= needed) { count -= (str[i] !== str[j]) k -= (str[i] !== '9') + (str[j] !== '9') str[i] = '9', str[j] = '9' } else { if (str[i] != str[j]) k--, count--; str[i] = str[j] = Math.max(str[i], str[j]); } // console.log(available, needed) } return str.join('')
Highest Value Palindrome
You are viewing a single comment's thread. Return to all comments →
JavaScript Solution