Count Number of Texts

by atalaykutlay

Solution to "Count Number of Texts" question on Leetcode.

Count Number of Texts
1class Solution:
2    def countTexts(self, pressedKeys: str) -> int:
3        dp = [0 for _ in range(len(pressedKeys)+1)]
4        dp[0] = 1
5        for i in range(1, len(pressedKeys) + 1):
6            dp[i] = dp[i-1]
7
8            if i >= 2:
9                if pressedKeys[i-1] == pressedKeys[i-2]:
10                    dp[i] += dp[i-2]
11            
12            if i >= 3:
13                if pressedKeys[i-1] == pressedKeys[i-2] == pressedKeys[i-3]:
14                    dp[i] += dp[i-3]
15            
16            if i>= 4:
17                if pressedKeys[i-1] == pressedKeys[i-2] == pressedKeys[i-3] == pressedKeys[i-4]:
18                    if pressedKeys[i-1] in ["7","9"]:
19                        dp[i] += dp[i-4]
20        
21        return dp[len(pressedKeys)] % (10**9 + 7)

Share

Share
Video
A cool 10 sec video.
Share
Detailed
Title, description, and syntax highlighted code.
Share
Simple
Syntax highlighted code with gradient background colored presentation.

Comments

You need to login to send a comment.
atalaykutlay 18 months ago
For some performance improvement, `dp[i] = dp[i] % (10**9 + 7)` can be added after each dp[i] change, to keep the number small.

Similar Snippets

Quicksort in Python
Quicksort in Python

An easy quicksort implementation in Python.

Language: python19 months ago
Second Minimum Node In a Binary Tree
Second Minimum Node In a Binary Tree

Solution to "Second Minimum Node In a Binary Tree" on Leetcode.

Language: python17 months ago
Combination Sum
Combination Sum

Solution for the Combination Sum problem on Leetcode.

Language: python19 months ago