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

📅  最后修改于: 2021-10-25 09:14:36             🧑  作者: 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


Javascript


输出:
hello

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程