Skip to content

Commit

Permalink
Create count-number-of-texts.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored May 8, 2022
1 parent 0707d72 commit 1ddd1d2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Python/count-number-of-texts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Time: O(n)
# Space: O(1)

# dp
class Solution(object):
def countTexts(self, pressedKeys):
"""
:type pressedKeys: str
:rtype: int
"""
MOD = 10**9+7
dp = [1]*5
for i in xrange(1, len(pressedKeys)+1):
dp[i%5] = 0
for j in reversed(xrange(max(i-(4 if pressedKeys[i-1] in "79" else 3), 0), i)):
if pressedKeys[j] != pressedKeys[i-1]:
break
dp[i%5] = (dp[i%5]+dp[j%5])%MOD
return dp[len(pressedKeys)%5]

0 comments on commit 1ddd1d2

Please sign in to comment.