leetcode 925. 长按键入
生活随笔
收集整理的這篇文章主要介紹了
leetcode 925. 长按键入
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目描述:
你的朋友正在使用鍵盤輸入他的名字?name。偶爾,在鍵入字符?c?時(shí),按鍵可能會(huì)被長(zhǎng)按,而字符可能被輸入 1 次或多次。
你將會(huì)檢查鍵盤輸入的字符?typed。如果它對(duì)應(yīng)的可能是你的朋友的名字(其中一些字符可能被長(zhǎng)按),那么就返回?True。
?
示例 1:
輸入:name = "alex", typed = "aaleex" 輸出:true 解釋:'alex' 中的 'a' 和 'e' 被長(zhǎng)按。示例 2:
輸入:name = "saeed", typed = "ssaaedd" 輸出:false 解釋:'e' 一定需要被鍵入兩次,但在 typed 的輸出中不是這樣。示例 3:
輸入:name = "leelee", typed = "lleeelee" 輸出:true示例 4:
輸入:name = "laiden", typed = "laiden" 輸出:true 解釋:長(zhǎng)按名字中的字符并不是必要的。?
提示:
思路:如示例3,當(dāng)name和typed遇到相同且重復(fù)的字符時(shí),先嘗試同時(shí)向后移動(dòng),若不可以再嘗試把typed向后移動(dòng)。
class Solution { public:bool isLongPressedName(string name, string typed) {int i,j;for(i = 0,j=0;i<name.size();i++,j++){if(name[i] == typed[j]){while(name[i]==name[i+1] ){if(typed[j]==typed[j+1]){i++;j++;}elsereturn false;}while(typed[j] == typed[j+1]){j++;}}elsereturn false;}bool ans;if(i == name.size()){ans = true;}elseans = false;return ans;} };題目鏈接:https://leetcode-cn.com/problems/long-pressed-name/
轉(zhuǎn)載于:https://www.cnblogs.com/hdyss/p/10800653.html
總結(jié)
以上是生活随笔為你收集整理的leetcode 925. 长按键入的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。