Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created January 22, 2019 03:59
Show Gist options
  • Save kanrourou/6ab5e0f380022d79f7762a3b5ae391bf to your computer and use it in GitHub Desktop.
Save kanrourou/6ab5e0f380022d79f7762a3b5ae391bf to your computer and use it in GitHub Desktop.
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