#力扣 LeetCode925. 長按鍵入 @FDDLC
阿新 • 來源:網路 • 發佈:2021-02-20
技術標籤:演算法&資料結構
題目描述:
https://leetcode-cn.com/problems/long-pressed-name/
Java程式碼:
class Solution { //name 和 typed 的字元都是小寫字母
public boolean isLongPressedName(String n, String t) {
int ni=n.length()-1,ti=t.length()-1;
for(int nch,tch,ncnt,tcnt;ni>=0&&ti>=0;){
for(ncnt=0,nch=n.charAt(ni);ni>=0&&n.charAt(ni)==nch;ncnt++,ni--);
for(tcnt=0,tch=t.charAt(ti);ti>=0&&t.charAt(ti)==tch;tcnt++,ti--);
if(nch!=tch||ncnt>tcnt)return false;
}
return ni==-1&&ti==-1;
}
}

