관리 메뉴

솜씨좋은장씨

[leetCode] 925. Long Pressed Name (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 925. Long Pressed Name (Python)

솜씨좋은장씨 2020. 9. 25. 22:59
728x90
반응형

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.

Example 3:

Input: name = "leelee", typed = "lleeelee"
Output: true

Example 4:

Input: name = "laiden", typed = "laiden"
Output: true
Explanation: It's not necessary to long press any character.

Constraints:

  • 1 <= name.length <= 1000
  • 1 <= typed.length <= 1000
  • The characters of name and typed are lowercase letters.

Solution

class Solution:
    def isLongPressedName(self, name: str, typed: str) -> bool:
        cnt = 0
        
        answer = False
        for i in range(len(typed)):
            if cnt < len(name) and name[cnt] == typed[i]:
                cnt = cnt + 1
            elif i == 0 or typed[i] != typed[i-1]:
                return answer
            
        if cnt == len(name):
            answer = True
            
        return answer

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments