Leetcode Note: Go - Long Pressed Name

Long Pressed Name - LeetCode
https://leetcode.com/problems/long-pressed-name/

  • Go 言語で取り組んだメモ

回答

Go - Two pointer - LeetCode Discuss
https://leetcode.com/problems/long-pressed-name/discuss/2099093/Go-Two-pointer

func isLongPressedName(name string, typed string) bool {
    if len(name) > len(typed) {
        return false
    }
    nameChars := []byte(name)
    typedChars := []byte(typed)
    
    i, j := 0, 0
    
    for j < len(typedChars) && i < len(nameChars) {
        if nameChars[i] == typedChars[j] {
            i++
            j++
        } else if isLongPressed(typedChars, j) {
            j++
        } else {
            return false
        }
    }
    
    // increment j until longpressed chars found
    for j < len(typedChars) {
        if isLongPressed(typedChars, j) {
            j++
        } else {
            return false
        }
    }
    
    return i == len(name) && j == len(typed)

}

func isLongPressed(typed []byte, i int) bool {
    return i > 0 && typed[i] == typed[i - 1]
}