Two Pointer Method

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值