LeetCode: Ambiguous Coordinates Solution
1/**2 * @param {string} s3 * @return {string[]}4 */5var ambiguousCoordinates = function (s) {6 s = s.substring(1, s.length - 1)78 const generate = s => {9 if (s.length === 1) return [s]10 if (s.startsWith("0") && s.endsWith("0")) return []11 if (s.startsWith("0")) return [`0.${s.substring(1)}`]12 if (s.endsWith("0")) return [s]1314 const possible = [s]15 for (let i = 1; i < s.length; i++) {16 possible.push(`${s.substring(0, i)}.${s.substring(i)}`)17 }18 return possible19 }2021 const makePair = (x, y) => `(${x}, ${y})`2223 const res = []2425 for (let i = 1; i < s.length; i++) {26 for (const x of generate(s.substring(0, i))) {27 for (const y of generate(s.substring(i))) {28 res.push(makePair(x, y))29 }30 }31 }3233 return res34}
Comments
Loading comments...
Tags
leetcode
string
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.
Next Post
LeetCode: Flatten Binary Tree To Linked List
May 14, 2021
Previous Post
LeetCode: Range Sum Query 2d Immutable
May 12, 2021