LeetCode——長按鍵入
阿新 • • 發佈:2020-10-21
題目地址: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++; elsebreak; } } if (i != lenName) return false; while (j < lenTyped) if (typed[j] == typed[j - 1]) j++; else break; if (j != lenTyped) returnfalse; else return true; } };