Leetcode--925. 长按键入
你的朋友正在使用鍵盤輸入他的名字?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)按名字中的字符并不是必要的。
?
提示:
name.length <= 1000
typed.length <= 1000
name 和?typed?的字符都是小寫字母。
?
思路:雙指針?lè)?/p>
1.如果name[i]==typed[i],都向后遍歷一下
2.如果name[i]!=typed[i],但是typed[i]==typed[i-1],typed向后遍歷一下
3.如果name[i]!=typed[i],typed[i]!=typed[i-1],return false;
提交的代碼:
class Solution {
? ? public boolean isLongPressedName(String name, String typed) {
? ? ? ?if(typed.length()==0)
?? ??? ? {
?? ??? ??? ? return false;
?? ??? ? }
?? ??? ? int i,j;
?? ??? ? for(i=0,j=0;i<name.length()&&j<typed.length();)
?? ??? ? {
?? ??? ??? ? if(name.charAt(i)==typed.charAt(j))
?? ??? ??? ? {
?? ??? ??? ??? ? i++;
?? ??? ??? ??? ? j++;
?? ??? ??? ? }
?? ??? ??? ? else
?? ??? ??? ? {
?? ??? ??? ??? ? if(j!=0&&typed.charAt(j)==typed.charAt(j-1))
?? ??? ??? ??? ? {
?? ??? ??? ??? ??? ? j++;
?? ??? ??? ??? ? }
?? ??? ??? ??? ? else if(j!=0&&typed.charAt(j)!=typed.charAt(j-1))
?? ??? ??? ??? ? {
?? ??? ??? ??? ??? ? return false;
?? ??? ??? ??? ? }
?? ??? ??? ??? ? else if(j==0)
?? ??? ??? ??? ? {
?? ??? ??? ??? ??? ? return false;
?? ??? ??? ??? ? }
?? ??? ??? ? }
?? ??? ? }
?? ??? ? if(j!=typed.length())
?? ??? ? {
?? ??? ??? ? while(j<typed.length()&&typed.charAt(j)==typed.charAt(j-1))
?? ??? ??? ? {
?? ??? ??? ??? ? j++;
?? ??? ??? ? }
?? ??? ? }
?? ??? ? if(i!=name.length()||j!=typed.length())
?? ??? ? {
?? ??? ??? ? return false;
?? ??? ? }
?? ??? ? return true;
?? ? ? ? ? ?
? ? }
}
完整的代碼:
public class Solution925 {
?? ? public static boolean isLongPressedName(String name, String typed) {
?? ??? ? if(typed.length()==0)
?? ??? ? {
?? ??? ??? ? return false;
?? ??? ? }
?? ??? ? int i,j;
?? ??? ? for(i=0,j=0;i<name.length()&&j<typed.length();)
?? ??? ? {
?? ??? ??? ? if(name.charAt(i)==typed.charAt(j))
?? ??? ??? ? {
?? ??? ??? ??? ? i++;
?? ??? ??? ??? ? j++;
?? ??? ??? ? }
?? ??? ??? ? else
?? ??? ??? ? {
?? ??? ??? ??? ? if(j!=0&&typed.charAt(j)==typed.charAt(j-1))
?? ??? ??? ??? ? {
?? ??? ??? ??? ??? ? j++;
?? ??? ??? ??? ? }
?? ??? ??? ??? ? else if(j!=0&&typed.charAt(j)!=typed.charAt(j-1))
?? ??? ??? ??? ? {
?? ??? ??? ??? ??? ? return false;
?? ??? ??? ??? ? }
?? ??? ??? ??? ? else if(j==0)
?? ??? ??? ??? ? {
?? ??? ??? ??? ??? ? return false;
?? ??? ??? ??? ? }
?? ??? ??? ? }
?? ??? ? }
?? ??? ? if(j!=typed.length())
?? ??? ? {
?? ??? ??? ? while(j<typed.length()&&typed.charAt(j)==typed.charAt(j-1))
?? ??? ??? ? {
?? ??? ??? ??? ? j++;
?? ??? ??? ? }
?? ??? ? }
?? ??? ? if(i!=name.length()||j!=typed.length())
?? ??? ? {
?? ??? ??? ? return false;
?? ??? ? }
?? ??? ? return true;
?? ? ? ? ? ?
?? ? ? ?}
?? ? public static void main(String[] args)
?? ? {
?? ??? ? //String a="pyplrz";
?? ??? ? //String b="ppyypllr";
?? ??? ? String a = "vtkgn";
?? ??? ? String b = "vttkgnn";
?? ??? ? System.out.println(isLongPressedName(a,b));
?? ? }
}
?
?
總結(jié)
以上是生活随笔為你收集整理的Leetcode--925. 长按键入的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【剑指offer】面试题33:二叉搜索树
- 下一篇: Leetcode--448. 找到所有数