📌  相关文章
📜  通过删除一对相同的相邻字符将字符串减少到最短长度

📅  最后修改于: 2022-05-13 01:57:59.344000             🧑  作者: Mango

通过删除一对相同的相邻字符将字符串减少到最短长度

给定一个由小写字符组成的字符串str 。任务是计算将字符串减少到最短长度所需的删除次数。在每次删除操作中,您可以选择一对相邻的匹配的小写字母,然后将它们删除。任务是打印完成的删除计数。

例子:

Input: str = "aaabccddd"
Output: 3
Following are sequence of operations:
aaabccddd -> abccddd -> abddd -> abd

Input: str = "aa"
Output: 1

方法:

  1. 最初初始化count = 1。
  2. 迭代每个字符,如果 s[i]==s[i-1]增加计数。
  3. 如果 s[i]!=s[i-1] ,将 count/2 添加到步数,并将 count 重新初始化为 1。

如果 s[i]!=s[i-1],则删除次数增加 count/2。如果计数是偶数,则对数将为计数/2。如果count是奇数,则删除的数量将为 (count-1)/2,与 (int)count/2 相同。

下面是上述方法的实现:

C++
// C++ program to count deletions
// to reduce the string to its shortest
// length by deleting a pair of
// same adjacent characters
#include 
using namespace std;
 
// Function count the operations
int reduceString(string s, int l)
{
 
    int count = 1, steps = 0;
 
    // traverse in the string
    for (int i = 1; i < l; i++) {
        // if adjacent characters are same
        if (s[i] == s[i - 1])
            count += 1;
 
        else {
            // if same adjacent pairs are more than 1
         
                steps += (count / 2);
 
            count = 1;
        }
    }
 
     
        steps += count / 2;
    return steps;
}
 
// Driver Code
int main()
{
 
    string s = "geeksforgeeks";
     
    int l = s.length();
    cout << reduceString(s, l) << endl;
    return 0;
}


Java
// Java program to count deletions
// to reduce the string to its
// shortest length by deleting a
// pair of same adjacent characters
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG
{
     
// Function count
// the operations
static int reduceString(String s,
                        int l)
{
 
    int count = 1, steps = 0;
 
    // traverse in the string
    for (int i = 1; i < l; i++)
    {
        // if adjacent characters
        // are same
        if (s.charAt(i) == s.charAt(i - 1))
            count += 1;
 
        else
        {
            // if same adjacent pairs
            // are more than 1
            steps += (count / 2);
 
            count = 1;
        }
    }
        steps += count / 2;
    return steps;
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "geeksforgeeks";
     
    int l = s.length();
    System.out.print(reduceString(s, l) + "\n");
}
}


Python3
# Python3 program to count
# deletions to reduce
# the string to its
# shortest length by
# deleting a pair of
# same adjacent characters
  
# Function count
# the operations
def reduceString(s, l):
    count = 1;
    steps = 0;
  
    # traverse in
    # the string
    for i in range(1,l):
        # if adjacent
        # characters are same
        if (s[i] is s[i - 1]):
            count += 1;
  
        else:
            # if same adjacent pairs
            # are more than 1
            steps +=(int)(count / 2);
  
            count = 1;
        steps +=(int)(count / 2);
    return steps;
 
  
# Driver Code
s = "geeksforgeeks";
  
l = len(s);
print(reduceString(s, l));
 
 
# This code contributed by Rajput-Ji


C#
// C# program to count deletions
// to reduce the string to its
// shortest length by deleting a
// pair of same adjacent characters
using System;
 
class GFG
{
     
// Function count
// the operations
static int reduce(string s,
                  int l)
{
 
    int count = 1, step = 0;
 
    // traverse in
    // the string
    for (int i = 1; i < l; i++)
    {
        // if adjacent characters
        // are same
        if (s[i] == s[i - 1])
            count += 1;
 
        else
        {
            // if same adjacent pairs
            // are more than 1
            step += (count / 2);
            count = 1;
        }
    }
        step += count / 2;
    return step;
}
 
// Driver Code
public static void Main()
{
    string s = "geeksforgeeks";
     
    int l = s.Length;
    Console.WriteLine(reduce(s, l));
}
}
 
// This code is contributed by
// Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
2