Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool backspaceCompare(string s, string t) {
- int i=s.length()-1,cnt1=0;
- int j=t.length()-1,cnt2=0;
- while(i>=0 && j>=0)
- {
- if(s[i]=='#'){
- cnt1++;
- i--;
- continue;
- }
- if(t[j]=='#'){
- cnt2++;
- j--;
- continue;
- }
- if(cnt1)
- {
- cnt1--;
- i--;
- continue;
- }
- if(cnt2)
- {
- cnt2--;
- j--;
- continue;
- }
- if(s[i]!=t[j])
- return 0;
- i--;
- j--;
- }
- while(i>=0){
- if(s[i]=='#'){
- cnt1++;
- }
- else if(cnt1)
- cnt1--;
- else
- return 0;
- i--;
- }
- while(j>=0){
- if(t[j]=='#'){
- cnt2++;
- }
- else if(cnt2)
- cnt2--;
- else
- return 0;
- j--;
- }
- return 1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment