문제
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.
Note:
name.length <= 1000 typed.length <= 1000 The characters of name and typed are lowercase letters.
풀이
처음에 이 문제 풀이 방식이 생각이 안나서, 네번정도 삽질을 하고 다시 풀고를 반복했다..
맵 2개를 만들어서 알파벳길이를 넣고 비교도 해보고 했는데 안되서 다시한번 곰곰히 생각을 해봤다.
근데 잘 생각해보니, name에 들어있는 문자 하나하나가 typed에 있는지 검증하면 되겠다는 생각이 들었다.
그리하여 포인터 두개를 써야겠다는 생각이 들었고, 위 그림과 같이 풀게 되었다.
처음에 어떻게 풀이 방식을 잡고 가냐에 따라서 풀수 있느냐 없느냐가 결정되는것같다.
/**
* @param {string} name
* @param {string} typed
* @return {boolean}
*/
// name : alex, typed : aaalexx
var isLongPressedName = function(name, typed) {
let i = 0,
j = 0;
while (i < name.length && j < typed.length) {
if (name[i] === typed[j]) {
i++;
}
j++;
}
if (i === name.length) return true;
else return false;
};