1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| function backspaceCompare(s: string, t: string): boolean { let sarr = s.split(''); let tarr = t.split(''); let slowIndexs = 0, fastIndexs = 0; let slowIndext = 0, fastIndext = 0; const Sharp = '#'; while(fastIndexs < sarr.length){ if(sarr[fastIndexs] === Sharp){ if(slowIndexs !== 0){ slowIndexs--; } }else{ sarr[slowIndexs++] = sarr[fastIndexs]; } fastIndexs++; } while(fastIndext < tarr.length){ if(tarr[fastIndext] === Sharp){ if(slowIndext !== 0){ slowIndext--; } }else{ tarr[slowIndext++] = tarr[fastIndext]; } fastIndext++; } if(slowIndexs !== slowIndext){ return false; } for(let i=0;i<slowIndexs;i++){ if(sarr[i] !== tarr[i]){ return false; } } return true; };
|