-
Notifications
You must be signed in to change notification settings - Fork 0
/
isLongPressedName.py
48 lines (40 loc) · 1.46 KB
/
isLongPressedName.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
Source : https://leetcode.com/problems/long-pressed-name/description/
Author : Yuan Wang
Date : 2019-01-12
/**********************************************************************************
*Your friend is typing his name into a keyboard. Sometimes, when typing a character
*c, the key might get long pressed, and the character will be typed 1 or more times.
*
*You examine the typed characters of the keyboard. Return True if it is possible that
*it was your friends name, with some characters (possibly none) being long pressed.
*
*Example 1:
*
*Input: name = "alex", typed = "aaleex"
*Output: true
*Explanation: 'a' and 'e' in 'alex' were long pressed.
*Example 2:
*
*Input: name = "saeed", typed = "ssaaedd"
*Output: false
*Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.
**********************************************************************************/
'''
import itertools
def isLongPressedName(name, typed):
g1 = [(k, len(list(grp))) for k, grp in itertools.groupby(name)]
g2 = [(k, len(list(grp))) for k, grp in itertools.groupby(typed)]
if len(g1) != len(g2):
return False
return all(k1 == k2 and v1 <= v2
for (k1,v1), (k2,v2) in zip(g1, g2))
import unittest
class Test(unittest.TestCase):
def setUp(self):
self.name = "alex"
self.typed = "aaleex"
def test(self):
self.assertEqual(isLongPressedName(self.name,self.typed),True)
if __name__ == '__main__':
unittest.main()