📌  相关文章
📜  从字符串中删除给定子序列的最大数量

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

从字符串中删除给定子序列的最大数量

给定字符串str ,任务是计算可以在str上执行的最大可能操作数。操作包括从字符串中获取子序列“gks”并将其从字符串中删除。

例子:

Input: str = "ggkssk"
Output: 1
After 1st operation: str = "gsk"
No further operation can be performed.

Input: str = "kgs"
Output: 0

方法:

  1. 取三个变量ggkgks ,它们将分别存储子序列'g''gk''gks' 的出现
  2. 字符字符字符串:
    • 如果str[i] = 'g'则更新g = g + 1
    • 如果str[i] = 'k'g > 0则更新g = g – 1gk = gk + 1如之前发现的 'g'现在与当前的 'k ' 一起贡献给子序列'gk ' 。
    • 同样,如果str[i] = 's'并且gk > 0则更新gk = gk – 1gks = gks + 1
  3. 最后打印gks的值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return max possible operation
// of the given type that can be performed on str
int maxOperations(string str)
{
    int i, g, gk, gks;
    i = g = gk = gks = 0;
    for (i = 0; i < str.length(); i++) {
        if (str[i] == 'g') {
 
            // Increment count of sub-sequence 'g'
            g++;
        }
        else if (str[i] == 'k') {
 
            // Increment count of sub-sequence 'gk'
            // if 'g' is available
            if (g > 0) {
                g--;
                gk++;
            }
        }
        else if (str[i] == 's') {
 
            // Increment count of sub-sequence 'gks'
            // if sub-sequence 'gk' appeared previously
            if (gk > 0) {
                gk--;
                gks++;
            }
        }
    }
 
    // Return the count of sub-sequence 'gks'
    return gks;
}
 
// Driver code
int main()
{
    string a = "ggkssk";
    cout << maxOperations(a);
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
// Function to return max possible
// operation of the given type that
// can be performed on str
static int maxOperations(String str)
{
    int i, g, gk, gks;
    i = g = gk = gks = 0;
    for (i = 0; i < str.length(); i++)
    {
        if (str.charAt(i) == 'g')
        {
 
            // Increment count of sub-sequence 'g'
            g++;
        }
        else if (str.charAt(i) == 'k')
        {
 
            // Increment count of sub-sequence 'gk'
            // if 'g' is available
            if (g > 0) {
                g--;
                gk++;
            }
        }
        else if (str.charAt(i) == 's')
        {
 
            // Increment count of sub-sequence 'gks'
            // if sub-sequence 'gk' appeared previously
            if (gk > 0)
            {
                gk--;
                gks++;
            }
        }
    }
 
    // Return the count of sub-sequence 'gks'
    return gks;
}
 
// Driver code
public static void main(String args[])
{
    String a = "ggkssk";
    System.out.print(maxOperations(a));
}
}
 
// This code is contributed
// by Akanksha Rai


Python 3
# Python 3 implementation of the approach
 
# Function to return max possible operation
# of the given type that can be performed
# on str
def maxOperations( str):
 
    i, g, gk, gks = 0, 0, 0, 0
    for i in range(len(str)) :
        if (str[i] == 'g') :
 
            # Increment count of sub-sequence 'g'
            g += 1
         
        elif (str[i] == 'k') :
 
            # Increment count of sub-sequence
            # 'gk', if 'g' is available
            if (g > 0) :
                g -= 1
                gk += 1
             
        elif (str[i] == 's') :
 
            # Increment count of sub-sequence 'gks'
            # if sub-sequence 'gk' appeared previously
            if (gk > 0) :
                gk -= 1
                gks += 1
 
    # Return the count of sub-sequence 'gks'
    return gks
 
# Driver code
if __name__ == "__main__":
     
    a = "ggkssk"
    print(maxOperations(a))
 
# This code is contributed by ita_c


C#
// C# implementation of the approach
using System ;
 
public class GFG{
    // Function to return max possible operation
    // of the given type that can be performed on str
    static int maxOperations(string str)
    {
        int i, g, gk, gks;
        i = g = gk = gks = 0;
        for (i = 0; i < str.Length; i++) {
            if (str[i] == 'g') {
     
                // Increment count of sub-sequence 'g'
                g++;
            }
            else if (str[i] == 'k') {
     
                // Increment count of sub-sequence 'gk'
                // if 'g' is available
                if (g > 0) {
                    g--;
                    gk++;
                }
            }
            else if (str[i] == 's') {
     
                // Increment count of sub-sequence 'gks'
                // if sub-sequence 'gk' appeared previously
                if (gk > 0) {
                    gk--;
                    gks++;
                }
            }
        }
     
        // Return the count of sub-sequence 'gks'
        return gks;
    }
     
    // Driver code
    public static void Main()
    {
        string a = "ggkssk";
        Console.WriteLine(maxOperations(a)) ;
     
    }
     
}


PHP
 0)
            {
                $g--;
                $gk++;
            }
        }
        else if ($str[$i] == 's')
        {
 
            // Increment count of sub-sequence 'gks'
            // if sub-sequence 'gk' appeared previously
            if ($gk > 0)
            {
                $gk--;
                $gks++;
            }
        }
    }
 
    // Return the count of sub-sequence 'gks'
    return $gks;
}
 
// Driver code
$a = "ggkssk";
echo maxOperations($a);
 
// This code is contributed
// by Akanksha Rai
?>


Javascript


输出:
1