classSolution {publicbooleanbackspaceCompare(String S,String T) {int i =S.length() -1, j =T.length() -1;int skipS =0, skipT =0;while (i >=0|| j >=0) { // While there may be chars in build(S) or build (T)while (i >=0) { // Find position of next possible char in build(S)if (S.charAt(i) =='#') {skipS++; i--;}elseif (skipS >0) {skipS--; i--;}elsebreak; }while (j >=0) { // Find position of next possible char in build(T)if (T.charAt(j) =='#') {skipT++; j--;}elseif (skipT >0) {skipT--; j--;}elsebreak; }// If two actual characters are differentif (i >=0&& j >=0&&S.charAt(i) !=T.charAt(j))returnfalse;// If expecting to compare char vs nothingif ((i >=0) != (j >=0))returnfalse; i--; j--; }returntrue; }}