Skip to content

Commit c485a98

Browse files
authored
Create check-if-numbers-are-ascending-in-a-sentence.py
1 parent 0750faa commit c485a98

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def areNumbersAscending(self, s):
6+
"""
7+
:type s: str
8+
:rtype: bool
9+
"""
10+
prev = curr = -1
11+
for i, c in enumerate(s):
12+
if c.isdigit():
13+
curr = max(curr, 0)*10+int(c)
14+
continue
15+
if prev != -1 and curr != -1 and prev >= curr:
16+
return False
17+
if curr != -1:
18+
prev = curr
19+
curr = -1
20+
return curr == -1 or prev < curr
21+
22+
23+
# Time: O(n)
24+
# Space: O(n)
25+
class Solution2(object):
26+
def areNumbersAscending(self, s):
27+
"""
28+
:type s: str
29+
:rtype: bool
30+
"""
31+
nums = [int(x) for x in s.split() if x.isdigit()]
32+
return all(nums[i] < nums[i+1] for i in xrange(len(nums)-1))

0 commit comments

Comments
 (0)