📌  相关文章
📜  根据给定条件更改给定字符串

📅  最后修改于: 2021-04-27 18:56:02             🧑  作者: Mango

给定字符串S,任务是更改不遵循下面给出的任何规则的字符串id,并打印更新的字符串。校对的规则是:

  1. 如果连续三个字符,则表示拼写错误。删除其中一个字符。例如,字符串“ ooops”可以更改为“ oops”
  2. 如果将两对相同的字符(AABB)连接在一起,则表示拼写错误。删除第二对字符之一。例如,字符串“ helloo”可以更改为“ hello”
  3. 规则遵循从左到右的优先级。

例子:

方法:想法是遍历字符串,如果拼写错误,请根据给定条件删除多余的字符。由于错误的优先级是从左到右,并且根据给定的规则,可以看出,拼写错误的判断不会发生冲突。考虑从左到右遍历,将已经合法的字符添加到结果中。步骤如下:

  • 初始化堆栈以存储字符并比较字符串的最后字符。
  • 遍历字符串并将字符添加到堆栈中。
  • 检查堆栈的最后3个字符(如果相同),然后在堆栈顶部弹出该字符。
  • 检查堆栈的最后4个字符(如果相同),然后在堆栈顶部弹出该字符。
  • 最后,返回堆栈的字符。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to proofread the spells
string proofreadSpell(string& str)
{
    vector result;
 
    // Loop to iterate over the
    // characters of the string
    for (char c : str) {
 
        // Push the current character c
        // in the stack
        result.push_back(c);
 
        int n = result.size();
 
        // Check for Rule 1
        if (n >= 3) {
            if (result[n - 1]
                    == result[n - 2]
                && result[n - 1]
                       == result[n - 3]) {
                result.pop_back();
            }
        }
        n = result.size();
 
        // Check for Rule 2
        if (n >= 4) {
            if (result[n - 1]
                    == result[n - 2]
                && result[n - 3]
                       == result[n - 4]) {
                result.pop_back();
            }
        }
    }
 
    // To store the resultant string
    string resultStr = "";
 
    // Loop to iterate over the
    // characters of stack
    for (char c : result) {
        resultStr += c;
    }
 
    // Return the resultant string
    return resultStr;
}
 
// Driver Code
int main()
{
    // Given string str
    string str = "helloo";
 
    // Function Call
    cout << proofreadSpell(str);
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to proofread the spells
public static String proofreadSpell(String str)
{
    Vector result = new Vector();
 
    // Loop to iterate over the
    // characters of the string
    for(int i = 0; i < str.length(); i++)
    {
         
        // Push the current character c
        // in the stack
        result.add(str.charAt(i));
 
        int n = result.size();
 
        // Check for Rule 1
        if (n >= 3)
        {
            if (result.get(n - 1) ==
                result.get(n - 2) &&
                result.get(n - 1) ==
                result.get(n - 3))
            {
                result.remove(result.size() - 1);
            }
        }
        n = result.size();
 
        // Check for Rule 2
        if (n >= 4)
        {
            if (result.get(n - 1) ==
                result.get(n - 2) &&
                result.get(n - 3) ==
                result.get(n - 4))
            {
                result.remove(result.size() - 1);
            }
        }
    }
 
    // To store the resultant string
    String resultStr = "";
 
    // Loop to iterate over the
    // characters of stack
    for(Character c : result)
    {
        resultStr += c;
    }
     
    // Return the resultant string
    return resultStr;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given string str
    String str = "helloo";
 
    // Function call
    System.out.println(proofreadSpell(str));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for
# the above approach
 
# Function to proofread
# the spells
def proofreadSpell(str):
   
    result = []
 
    # Loop to iterate over the
    # characters of the string
    for c in str:
 
        # Push the current character c
        # in the stack
        result.append(c)
        n = len(result)
 
        # Check for Rule 1
        if(n >= 3):
            if(result[n - 1] == result[n - 2] and
               result[n - 1] == result[n - 3]):
                result.pop()
        n = len(result)
 
        # Check for Rule 2
        if(n >= 4):
            if(result[n - 1] == result[n - 2] and
               result[n - 3] == result[n - 4]):
                result.pop()
 
    # To store the
    # resultant string
    resultStr = ""
 
    # Loop to iterate over the
    # characters of stack
    for c in result:
        resultStr += c
 
    # Return the resultant string
    return resultStr
 
# Driver Code
 
# Given string str
str = "helloo"
 
# Function Call
print(proofreadSpell(str))
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program for the above approach 
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
 
// Function to proofread the spells
static string proofreadSpell(string str)
{
     
    // ArrayList result=new ArrayList();
    List result = new List();
     
    // Loop to iterate over the
    // characters of the string
    foreach(char c in str)
    {
 
        // Push the current character c
        // in the stack
        result.Add(c);
 
        int n = result.Count;
 
        // Check for Rule 1
        if (n >= 3)
        {
            if (result[n - 1] == result[n - 2] &&
                result[n - 1] == result[n - 3])
            {
                result.RemoveAt(n - 1);
            }
        }
         
        n = result.Count;
 
        // Check for Rule 2
        if (n >= 4)
        {
            if (result[n - 1] == result[n - 2] &&
                result[n - 3] == result[n - 4])
            {
                result.RemoveAt(n - 1);
            }
        }
    }
 
    // To store the resultant string
    string resultStr = "";
 
    // Loop to iterate over the
    // characters of stack
    foreach(char c in result)
    {
        resultStr += c;
    }
 
    // Return the resultant string
    return resultStr;
}
 
// Driver code
public static void Main(string[] args)
{
     
    // Given string str
    string str = "helloo";
 
    // Function call
    Console.Write(proofreadSpell(str));
}
}
 
// This code is contributed by rutvik_56


输出:
hello

时间复杂度: O(N)
辅助空间: O(N)