LeetCode: Change Minimum Characters To Satisfy One Of Three Conditions Solution
1var minCharacters = function (a, b) {2 const convertToArrayOfCharCode = str =>3 str.split("").map((_, i) => str.charCodeAt(i) - 97)45 // min step for a < b6 const sol1 = (a, b) => {7 let res = Infinity89 // tricky part to make it strictly "<"10 for (let i = 1; i < 26; i++) {11 let count = 012 for (const ai of a) count += i <= ai13 for (const bi of b) count += i > bi14 res = Math.min(res, count)15 }16 return res17 }1819 // min step to make a = b20 const sol3 = (a, b) => {21 let res = Infinity2223 for (let i = 0; i < 26; i++) {24 let count = 025 for (const ai of a) count += i !== ai26 for (const bi of b) count += i !== bi27 res = Math.min(res, count)28 }29 return res30 }3132 a = convertToArrayOfCharCode(a)33 b = convertToArrayOfCharCode(b)34 return Math.min(sol1(a, b), sol1(b, a), sol3(a, b))35}
Comments
Loading comments...
Tags
leetcode
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.