Two Pointer Method

muz1lee發表於2020-12-27

Two Pointer Method

Leetcode 925

  1. Long Pressed Name
    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.

Examples

Input: name = "alex", typed = "aaleex"
Output: true
Explanation: 'a' and 'e' in 'alex' were long pressed.

Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.

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

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
name and typed contain only lowercase English letters.

Solutions

Here are some cases for when we are allowed to skip characters of typed.

  1. First we run a loop to move the two pointers along the strings, until we reach the end of either string .
  • For each character in name, if there is a match with the next character in typed , we` advance both pointers.
  • If they are mismatch , and it is the first character of the block in typed, the answer is False.
  • Else , discard all similar characters of typed coming up . The next different character coming must match .
  1. At the end of the loop, we would end up three cases:
  • If there is still some characters left unmatched in the name string , then we do not have a match
  • If there is still some characters left in the typed string, and all the remaining characters are resulted from the long press, then we still have a match .
  • Otherwise , if any of the remaining characters in the typed string is not redundant , then we do not have a match .
class Solution:
    def isLongPressedName(self, name: str, typed: str) -> bool:
        np, tp = 0, 0
        while np < len(name) and tp < len(typed):
            if name[np] == typed[tp]:
                np += 1
                tp += 1
            elif tp >= 1 and typed[tp] == typed[tp-1]:
                tp += 1
            else:
                return False
                
        if np != len(name):
            return False
        else:
            while tp < len(typed):
                if typed[tp] != typed[tp-1]:
                    return False
                tp += 1

        return True

advance two pointers , until we exhaust one of the strings.

  		while np < len(name) and tp < len(typed):
            if name[np] == typed[tp]:
                np += 1
                tp += 1
            elif tp >= 1 and typed[tp] == typed[tp-1]:
                tp += 1
            else:
                return False
  • If there is still some characters left unmatched in the origin string, then we don’t have a match. eg. name = “abc” typed = “aabb”

  • In the case that there are some redundant characters left in typed
    we could still have a match. eg. name = “abc” typed = “abccccc”

 		if np != len(name):
 				return False
 		else:
            while tp < len(typed):
                if typed[tp] != typed[tp-1]:
                    return False
                tp += 1

        # both strings have been consumed
        return True

Leetcode Solution Section

相關文章