Created
January 22, 2019 03:59
-
-
Save kanrourou/6ab5e0f380022d79f7762a3b5ae391bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool backspaceCompare(string S, string T) { | |
int len1 = S.size(), len2 = T.size(), i = len1 - 1, j = len2 - 1, cnt1 = 0, cnt2 = 0; | |
while(i >= 0 || j >= 0) | |
{ | |
if(S[i] == '#'){--i;++cnt1;continue;} | |
if(T[j] == '#'){--j;++cnt2;continue;} | |
if(cnt1){--i;--cnt1;continue;} | |
if(cnt2){--j;--cnt2;continue;} | |
if(i < 0 && j < 0)break; | |
else if(i >= 0 && j >= 0) | |
{ | |
if(S[i] == T[j]) | |
{ | |
--i; | |
--j; | |
} | |
else | |
return false; | |
} | |
else | |
return false; | |
} | |
return true; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment