LeetCode解法汇总2337. 移动片段得到字符串

目录链接:

力扣编程题-解法汇总_分享+记录-CSDN博客

GitHub同步刷题项目:

https://github.com/September26/java-algorithms

原题链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台


描述:

给你两个字符串 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' 和 '_' 组成

解题思路:

* 解题思路:

* 找到字符串,每个位置上的字符按照下面的规则来判断即可。

* 1.顺序一样

* 2.L左侧空格的数目,start要大于target

* 3.R右侧空格的数组,target要大于start

代码:

class Solution2337
{
public:
    vector<pair<char, int>> makePiarList(string s)
    {
        vector<pair<char, int>> v;
        int blankNum = 0;
        int index = 0;

        while (index < s.length())
        {
            if (s[index] == '_')
            {
                blankNum++;
            }
            else if (s[index] == 'L')
            {
                v.push_back(make_pair('L', blankNum));
            }
            else
            {
                v.push_back(make_pair('R', blankNum));
            }
            index++;
        }
        return v;
    }

    bool canChange(string start, string target)
    {
        vector<pair<char, int>> startList = makePiarList(start);
        vector<pair<char, int>> targetList = makePiarList(target);
        if (startList.size() != targetList.size())
        {
            return false;
        }
        if (startList.size() == 0)
        {
            return true;
        }
        // start中总的空格数量
        int startBlankSum = startList[startList.size() - 1].second;
        // target中总的空格数量
        int targetBlankSum = startList[startList.size() - 1].second;

        for (int i = 0; i < startList.size(); i++)
        {
            // 顺序应该一致
            if (startList[i].first != targetList[i].first)
            {
                return false;
            }
            if (startList[i].first == 'L')
            {
                if (startList[i].second < targetList[i].second)
                {
                    return false;
                }
                continue;
            }
            if (startList[i].first == 'R')
            {
                if (startBlankSum - startList[i].second < targetBlankSum - targetList[i].second)
                {
                    return false;
                }
                continue;
            }
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/AA5279AA/article/details/132405385