// 演算法：統計 'L'、'R'、'_' 的個數，答案 = |L - R| + U。
// Algorithm: count 'L', 'R', '_'; the answer is |L - R| + U.
// 正確性來自「所有 '_' 應走同一方向」的貪心觀察。
// Correctness follows from the greedy observation that all '_' go the same way.

#include <stdlib.h>  // 提供 abs() / provides abs()

int furthestDistanceFromOrigin(char* moves) {
    // 三個計數器：固定向左、固定向右、可自由選擇的底線。
    // Three counters: forced-left, forced-right, free underscores.
    int left = 0, right = 0, underscore = 0;

    // 逐字元掃描直到遇到字串結尾的 '\0'。
    // Walk char by char until we hit the C-string terminator '\0'.
    // 這裡 *moves 是「moves 目前指到的那個字元」，再把指標往後移一格。
    // Here *moves dereferences the pointer to read the current char, then we advance.
    for (char* p = moves; *p != '\0'; ++p) {
        if (*p == 'L') {            // 向左固定一步 / a forced left step
            ++left;                 // 左邊計數 +1 / bump the left counter
        } else if (*p == 'R') {     // 向右固定一步 / a forced right step
            ++right;                // 右邊計數 +1 / bump the right counter
        } else {                    // 其餘只會是 '_' / the only remaining char is '_'
            ++underscore;           // 自由步數 +1 / one more free step
        }
    }

    // |L - R| 表示固定步數造成的偏移，再加上 underscore 表示所有底線都投向同側。
    // |L - R| is the offset from forced moves; adding underscore sends every '_' the same way.
    return abs(left - right) + underscore;
}
