// 演算法與 C 版相同：計數 L、R、_，回傳 |L - R| + U。
// Same algorithm as the C version: count L, R, _, return |L - R| + U.
// 這裡用 range-for 與 std::abs 讓程式碼更貼近現代 C++ 風格。
// Here we use a range-for loop and std::abs for idiomatic modern C++.

#include <string>     // std::string
#include <cstdlib>    // std::abs (整數版本) / integer abs

class Solution {
public:
    int furthestDistanceFromOrigin(std::string moves) {
        // 初始化三個計數器為 0 / initialize the three counters to 0
        int left = 0, right = 0, underscore = 0;

        // range-for：依序拿到每一個字元的副本 c，不用寫索引。
        // range-for: gives us each character c in turn, no manual index needed.
        for (char c : moves) {
            if (c == 'L') {            // 固定向左 / forced left
                ++left;                // 左計數 +1 / bump left
            } else if (c == 'R') {     // 固定向右 / forced right
                ++right;               // 右計數 +1 / bump right
            } else {                   // 其餘必為 '_' / otherwise it must be '_'
                ++underscore;          // 自由步數 +1 / bump the free counter
            }
        }

        // std::abs 取整數絕對值；加上 underscore 代表所有 '_' 都朝同一側。
        // std::abs gives the integer magnitude; adding underscore aims every '_' the same way.
        return std::abs(left - right) + underscore;
    }
};
