Skip to content

Instantly share code, notes, and snippets.

@wushbin
Created April 1, 2020 04:54
Show Gist options
  • Select an option

  • Save wushbin/8e8f2745f67d9fc557fa622461c56b11 to your computer and use it in GitHub Desktop.

Select an option

Save wushbin/8e8f2745f67d9fc557fa622461c56b11 to your computer and use it in GitHub Desktop.
class Solution {
public boolean canTransform(String start, String end) {
int pt1 = 0, pt2 = 0;
int len1 = start.length();
int len2 = end.length();
while(pt1 < len1 || pt2 < len2) {
// find not X
while(pt1 < len1 && start.charAt(pt1) == 'X') {
pt1++;
}
// find not X
while(pt2 < len2 && end.charAt(pt2) == 'X') {
pt2++;
}
if (pt1 == len1 && pt2 == len2) {
return true;
}
if (pt1 == len1 || pt2 == len2) {
return false;
}
if (start.charAt(pt1) != end.charAt(pt2)) {
return false;
}
if (start.charAt(pt1) == 'L' && pt1 < pt2) {
return false;
}
if (start.charAt(pt1) == 'R' && pt1 > pt2) {
return false;
}
pt1++;
pt2++;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment