Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

移动片段得到字符串 #162

Open
yankewei opened this issue Aug 21, 2023 · 1 comment
Open

移动片段得到字符串 #162

yankewei opened this issue Aug 21, 2023 · 1 comment
Labels
中等 题目难度为中等 双指针 题目包含双指针解法

Comments

@yankewei
Copy link
Owner

给你两个字符串 start 和 target ,长度均为 n 。每个字符串 仅 由字符 'L'、'R' 和 '_' 组成,其中:

字符 'L' 和 'R' 表示片段,其中片段 'L' 只有在其左侧直接存在一个 空位 时才能向 左 移动,而片段 'R' 只有在其右侧直接存在一个 空位 时才能向 右 移动。
字符 '_' 表示可以被 任意 'L' 或 'R' 片段占据的空位。
如果在移动字符串 start 中的片段任意次之后可以得到字符串 target ,返回 true ;否则,返回 false 。

示例 1:

输入:start = "_L__R__R_", target = "L______RR"
输出:true
解释:可以从字符串 start 获得 target ,需要进行下面的移动:
- 将第一个片段向左移动一步,字符串现在变为 "L___R__R_" 。
- 将最后一个片段向右移动一步,字符串现在变为 "L___R___R" 。
- 将第二个片段向右移动三步,字符串现在变为 "L______RR" 。
可以从字符串 start 得到 target ,所以返回 true 。

示例 2:

输入:start = "R_L_", target = "__LR"
输出:false
解释:字符串 start 中的 'R' 片段可以向右移动一步得到 "_RL_" 。
但是,在这一步之后,不存在可以移动的片段,所以无法从字符串 start 得到 target 。

示例 3:

输入:start = "_R", target = "R_"
输出:false
解释:字符串 start 中的片段只能向右移动,所以无法从字符串 start 得到 target 。

提示:

  • n == start.length == target.length
  • 1 <= n <= 105
  • start 和 target 由字符 'L'、'R' 和 '_' 组成
@yankewei
Copy link
Owner Author

双指针

class Solution {

    /**
     * @param String $start
     * @param String $target
     * @return Boolean
     */
    function canChange($start, $target) {
        $length = strlen($start);
        $i = 0;
        $j = 0;

        while ($i < $length && $j < $length) {
            if ($start[$i] === '_') {
                $i++;
                continue;
            }

            if ($target[$j] === '_') {
                $j++;
                continue;
            }

            if ($start[$i] !== $target[$j]) {
                return false;
            }

            if ($start[$i] === 'L' && $i < $j) {
                return false;
            }

            if ($start[$i] === 'R' && $i > $j) {
                return false;
            }
            $i++;
            $j++;
        }

        while ($i < $length) {
            if ($start[$i] !== '_') {
                return false;
            }
            $i++;
        }

        while ($j < $length) {
            if ($target[$j] !== '_') {
                return false;
            }
            $j++;
        }

        return true;
    }
}

@yankewei yankewei added 中等 题目难度为中等 双指针 题目包含双指针解法 labels Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
中等 题目难度为中等 双指针 题目包含双指针解法
Projects
None yet
Development

No branches or pull requests

1 participant