Leetcode 793 Solution
This article provides solution to leetcode question 793 (swap-adjacent-in-lr-string)
Access this page by simply typing in "lcs 793" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/swap-adjacent-in-lr-string
Solution
class Solution {
public:
bool canTransform(string start, string end) {
if (start.size() != end.size())
return false;
int n = start.size();
vector<pair<char, int>> start2;
start2.reserve(start.size());
for (int i = 0; i < n; i++)
if (start[i] != 'X')
start2.push_back(make_pair(start[i], i));
vector<pair<char, int>> end2;
end2.reserve(end.size());
for (int i = 0; i < n; i++)
if (end[i] != 'X')
end2.push_back(make_pair(end[i], i));
for (int i = 0; i < n; i++)
{
if (start2[i].first != end2[i].first)
return false;
if (start2[i].first == 'L' && start2[i].second < end2[i].second)
return false;
if (start2[i].first == 'R' && start2[i].second > end2[i].second)
return false;
}
return true;
}
};