LeetCode: Ambiguous Coordinates Solution

1/**
2 * @param {string} s
3 * @return {string[]}
4 */
5var ambiguousCoordinates = function (s) {
6 s = s.substring(1, s.length - 1)
7
8 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]
13
14 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 possible
19 }
20
21 const makePair = (x, y) => `(${x}, ${y})`
22
23 const res = []
24
25 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 }
32
33 return res
34}

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

HoningJS

Search Posts