[LeetCode] Long Pressed Name

아르당·6일 전

LeetCode

목록 보기
200/213
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

친구가 키보드에 자신의 이름을 입력하고 있다. 때때로 c와 같은 문자를 입력할 때 키가 길게 눌린 상태로 입력되어 한 번 이상 입력될 수 있다.

입력된 문자를 분석하여, 입력된 문자 중 일부(또는 전혀 없을 수도 있음)가 친구의 이름일 가능성이 있다면 true를 반환해라.

Example

#1
Input: name = "alex", typed = "aaleex"
Output: true
Explanation: alex에 있는 a와 e는 길게 눌렸다.

#2
Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation: e가 두 번 입력해야 하는데 출력된 typed는 그렇지 않다.

Constraints

  • 1 <= name.length, typed.length <= 1000
  • name과 typed는 오직 영어 소문자로만 구성된다.

Solved

class Solution {
    public boolean isLongPressedName(String name, String typed) {
        int i = 0;
        int m = name.length();
        int n = typed.length();

        for(int j = 0; j < n; j++){
            if(i < m && name.charAt(i) == typed.charAt(j)){
                i++;
            }else if(j == 0 || typed.charAt(j) != typed.charAt(j - 1)){
                return false;
            }
        }

        return i == m;
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글