1. 程式人生 > 實用技巧 >LeetCode——長按鍵入

LeetCode——長按鍵入

題目地址:https://leetcode-cn.com/problems/long-pressed-name/

解題思路:暴力,判斷返回false的幾種情況。

class Solution {
public:
    bool isLongPressedName(string name, string typed) {
        int lenName, lenTyped;
        lenName = name.size();
        lenTyped = typed.size();
        int i = 0, j = 0;
        if (lenTyped < lenName || name[0
]!=typed[0]) return false; while (i < lenName&&j < lenTyped) { if (name[i] == typed[j]){ i++; j++; } else { if (typed[j] == typed[j - 1]) j++; else
break; } } if (i != lenName) return false; while (j < lenTyped) if (typed[j] == typed[j - 1]) j++; else break; if (j != lenTyped) return
false; else return true; } };