📌  相关文章
📜  删除所有相似相邻字符对后可能的最短字符串

📅  最后修改于: 2021-09-04 11:24:38             🧑  作者: Mango

给定一个字符串S ,任务是找到可能的结果最小字符串,通过重复删除相等的相邻字符对形成。

例子:

方法:解决给定问题的主要思想是递归删除所有相等的相邻字符对,一一删除。按照步骤解决给定的问题:

  • 初始化一个空字符串,比如ans ,在删除所有相等的相邻字符对后存储最小长度的字符串。
  • 初始化一个字符串,比如pre ,在每次删除相等的相邻字符后存储更新的字符串。
  • 现在,迭代直到字符串anspre不相等并执行以下步骤:
    • 通过使用函数removeAdjacent()删除第一个相邻的相同字符来更新字符串ans的值。
    • 如果字符串ans的值与字符串pre相同,则跳出循环。否则,将字符串pre的值更新为字符串ans
  • 完成上述步骤后,将字符串ans打印为结果字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to delete pair of adjacent
// characters which are equal
string removeAdjacent(string s)
{
     
    // Base Case
    if (s.length() == 1)
        return s;
 
    // Stores the update string
    string sb = "";
 
    // Traverse the string s
    for(int i = 0; i < s.length() - 1; i++)
    {
        char c = s[i];
        char d = s[i + 1];
 
        // If two unequal pair of
        // adjacent characters are found
        if (c != d)
        {
            sb = sb + c;
 
            if (i == s.length() - 2)
            {
                sb = sb + d;
            }
        }
 
        // If two equal pair of adjacent
        // characters are found
        else
        {
            for(int j = i + 2;
                    j < s.length(); j++)
 
                // Append the remaining string
                // after removing the pair
                sb = sb + s[j];
 
            return sb;
        }
    }
 
    // Return the final String
    return sb;
}
 
// Function to find the shortest string
// after repeatedly removing pairs of
// adjacent characters which are equal
void reduceString(string s)
{
     
    // Stores the resultant String
    string result = "";
 
    // Keeps track of previously
    // iterated string
    string pre = s;
 
    while (true)
    {
         
        // Update the result after
        // deleting adjacent pair of
        // characters which are similar
        result = removeAdjacent(pre);
 
        // Termination Conditions
        if (result == pre)
            break;
 
        // Update pre variable with
        // the value of result
        pre = result;
    }
 
    if (result.length() != 0)
        cout << (result) << endl;
 
    // Case for "Empty String"
    else
        cout << "Empty String" << endl;
}
 
// Driver code   
int main()
{
    string S = "aaabccddd";
     
    reduceString(S);
     
    return 0;
}
 
// This code is contributed by divyesh072019


Java
// Java program for the above approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Function to delete pair of adjacent
    // characters which are equal
    static String removeAdjacent(String s)
    {
        // Base Case
        if (s.length() == 1)
            return s;
 
        // Stores the update string
        StringBuilder sb = new StringBuilder("");
 
        // Traverse the string s
        for (int i = 0;
             i < s.length() - 1; i++) {
            char c = s.charAt(i);
            char d = s.charAt(i + 1);
 
            // If two unequal pair of
            // adjacent characters are found
            if (c != d) {
                sb.append(c);
 
                if (i == s.length() - 2) {
                    sb.append(d);
                }
            }
 
            // If two equal pair of adjacent
            // characters are found
            else {
                for (int j = i + 2;
                     j < s.length(); j++)
 
                    // Append the remaining string
                    // after removing the pair
                    sb.append(s.charAt(j));
 
                return sb.toString();
            }
        }
 
        // Return the final String
        return sb.toString();
    }
 
    // Function to find the shortest string
    // after repeatedly removing pairs of
    // adjacent characters which are equal
    public static void reduceString(String s)
    {
        // Stores the resultant String
        String result = "";
 
        // Keeps track of previously
        // iterated string
        String pre = s;
 
        while (true) {
            // Update the result after
            // deleting adjacent pair of
            // characters which are similar
            result = removeAdjacent(pre);
 
            // Termination Conditions
            if (result.equals(pre))
                break;
 
            // Update pre variable with
            // the value of result
            pre = result;
        }
 
        if (result.length() != 0)
            System.out.println(result);
 
        // Case for "Empty String"
        else
            System.out.println("Empty String");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "aaabccddd";
        reduceString(S);
    }
}


C#
// C# program for the above approach
using System;
class GFG {
     
    // Function to delete pair of adjacent
    // characters which are equal
    static string removeAdjacent(string s)
    {
          
        // Base Case
        if (s.Length == 1)
            return s;
      
        // Stores the update string
        string sb = "";
      
        // Traverse the string s
        for(int i = 0; i < s.Length - 1; i++)
        {
            char c = s[i];
            char d = s[i + 1];
      
            // If two unequal pair of
            // adjacent characters are found
            if (c != d)
            {
                sb = sb + c;
      
                if (i == s.Length - 2)
                {
                    sb = sb + d;
                }
            }
      
            // If two equal pair of adjacent
            // characters are found
            else
            {
                for(int j = i + 2;
                        j < s.Length; j++)
      
                    // Append the remaining string
                    // after removing the pair
                    sb = sb + s[j];
      
                return sb;
            }
        }
      
        // Return the final String
        return sb;
    }
      
    // Function to find the shortest string
    // after repeatedly removing pairs of
    // adjacent characters which are equal
    static void reduceString(string s)
    {
          
        // Stores the resultant String
        string result = "";
      
        // Keeps track of previously
        // iterated string
        string pre = s;
      
        while (true)
        {
              
            // Update the result after
            // deleting adjacent pair of
            // characters which are similar
            result = removeAdjacent(pre);
      
            // Termination Conditions
            if (result == pre)
                break;
      
            // Update pre variable with
            // the value of result
            pre = result;
        }
      
        if (result.Length != 0)
            Console.WriteLine(result);
      
        // Case for "Empty String"
        else
            Console.WriteLine("Empty String");
    }
 
  static void Main() {
    string S = "aaabccddd";
      
    reduceString(S);
  }
}


Javascript


输出:
abd

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live