Ambiguous Coordinates LeetCode solution in Python

1
# Runtime: 129 ms (Top 6.42%) | Memory: 13.9 MB (Top 92.66%)
2
class Solution(object):
3
def ambiguousCoordinates(self, s):
4
"""
5
:type s: str
6
:rtype: List[str]
7
"""
8

9
def _isValidSplit(s):
10
return False if len(s) > 1 and re.match("/^[0]+$/", s) else True
11

12
def _isValidNum(ipart, fpart):
13
return (
14
False
15
if (len(ipart) > 1 and ipart[0] == "0") or (fpart and fpart[-1] == "0")
16
else True
17
)
18

19
def _splitToNums(s):
20
rets = []
21
if len(s) == 1:
22
return [s]
23
for i in range(1, len(s) + 1):
24
a, b = s[:i], s[i:]
25
if _isValidNum(a, b):
26
rets.append("%s.%s" % (a, b) if b else "%s" % (a))
27
return rets
28

29
ans, s = [], s[1:-1]
30
for i in range(1, len(s)):
31
a, b = s[:i], s[i:]
32
if not _isValidSplit(a) or not _isValidSplit(b):
33
continue
34
for c1, c2 in itertools.product(_splitToNums(a), _splitToNums(b)):
35
ans.append("(%s, %s)" % (c1, c2))
36
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0