📌  相关文章
📜  通过删除由相同字符组成的子字符串来删除字符串的最小步骤

📅  最后修改于: 2021-09-17 16:07:34             🧑  作者: Mango

给定一个字符串str 。如果在一次操作中所有字符都相同,则只能删除一些连续的字符。任务是找到完全删除字符串所需的最少操作次数。
例子:

方法:该问题可以使用dp[l][r]是子串 s[l, l+1, l+2, …r] 的答案。那么我们有两种情况:

  • 子字符串的第一个字母与其余字母分开删除,然后dp[l][r] = 1 + dp[l+1][r]
  • 子串的第一个字母与其他字母一起被删除(两个字母必须相等),然后dp[l][r] = dp[l+1][i-1] + dp[i][r ] ,假设l ≤ i ≤ rs[i] = s[l]

以下两种情况可以与 memoization 一起递归调用,以避免重复函数调用。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
const int N = 10;
 
// Function to return the minimum number of
// delete operations
int findMinimumDeletion(int l, int r, int dp[N][N], string s)
{
 
    if (l > r)
        return 0;
    if (l == r)
        return 1;
    if (dp[l][r] != -1)
        return dp[l][r];
 
    // When a single character is deleted
    int res = 1 + findMinimumDeletion(l + 1, r, dp, s);
 
    // When a group of consecutive characters
    // are deleted if any of them matches
    for (int i = l + 1; i <= r; ++i) {
 
        // When both the characters are same then
        // delete the letters in between them
        if (s[l] == s[i])
            res = min(res, findMinimumDeletion(l + 1, i - 1, dp, s)
                               + findMinimumDeletion(i, r, dp, s));
    }
 
    // Memoize
    return dp[l][r] = res;
}
 
// Driver code
int main()
{
    string s = "abcddcba";
    int n = s.length();
    int dp[N][N];
    memset(dp, -1, sizeof dp);
    cout << findMinimumDeletion(0, n - 1, dp, s);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
    static int N = 10;
 
    // Function to return the minimum number of
    // delete operations
    static int findMinimumDeletion(int l, int r,
                            int dp[][], String s)
    {
 
        if (l > r)
        {
            return 0;
        }
        if (l == r)
        {
            return 1;
        }
        if (dp[l][r] != -1)
        {
            return dp[l][r];
        }
 
        // When a single character is deleted
        int res = 1 + findMinimumDeletion(l + 1, r, dp, s);
 
        // When a group of consecutive characters
        // are deleted if any of them matches
        for (int i = l + 1; i <= r; ++i)
        {
 
            // When both the characters are same then
            // delete the letters in between them
            if (s.charAt(l) == s.charAt(i))
            {
                res = Math.min(res, findMinimumDeletion(l + 1, i - 1, dp, s)
                        + findMinimumDeletion(i, r, dp, s));
            }
        }
 
        // Memoize
        return dp[l][r] = res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "abcddcba";
        int n = s.length();
        int dp[][] = new int[N][N];
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                dp[i][j] = -1;
            }
        }
        System.out.println(findMinimumDeletion(0, n - 1, dp, s));
    }
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to return the minimum
# number of delete operations
def findMinimumDeletion(l, r, dp, s):
 
    if l > r:
        return 0
    if l == r:
        return 1
    if dp[l][r] != -1:
        return dp[l][r]
 
    # When a single character is deleted
    res = 1 + findMinimumDeletion(l + 1, r,
                                     dp, s)
 
    # When a group of consecutive characters
    # are deleted if any of them matches
    for i in range(l + 1, r + 1):
 
        # When both the characters are same then
        # delete the letters in between them
        if s[l] == s[i]:
            res = min(res, findMinimumDeletion(l + 1, i - 1, dp, s) +
                           findMinimumDeletion(i, r, dp, s))
     
    # Memoize
    dp[l][r] = res
    return res
 
# Driver code
if __name__ == "__main__":
 
    s = "abcddcba"
    n = len(s)
    N = 10
    dp = [[-1 for i in range(N)]
              for j in range(N)]
    print(findMinimumDeletion(0, n - 1, dp, s))
 
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    static int N = 10;
 
    // Function to return the minimum number of
    // delete operations
    static int findMinimumDeletion(int l, int r,
                            int [,]dp, String s)
    {
 
        if (l > r)
        {
            return 0;
        }
        if (l == r)
        {
            return 1;
        }
        if (dp[l, r] != -1)
        {
            return dp[l, r];
        }
 
        // When a single character is deleted
        int res = 1 + findMinimumDeletion(l + 1, r, dp, s);
 
        // When a group of consecutive characters
        // are deleted if any of them matches
        for (int i = l + 1; i <= r; ++i)
        {
 
            // When both the characters are same then
            // delete the letters in between them
            if (s[l] == s[i])
            {
                res = Math.Min(res, findMinimumDeletion(l + 1, i - 1, dp, s)
                        + findMinimumDeletion(i, r, dp, s));
            }
        }
 
        // Memoize
        return dp[l,r] = res;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String s = "abcddcba";
        int n = s.Length;
        int [,]dp = new int[N, N];
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                dp[i, j] = -1;
            }
        }
        Console.WriteLine(findMinimumDeletion(0, n - 1, dp, s));
    }
}
 
// This code has been contributed by 29AjayKumar


PHP
 $r)
        return 0;
    if ($l == $r)
        return 1;
    if ($dp[$l][$r] != -1)
        return $dp[$l][$r];
 
    // When a single character is deleted
    $res = 1 + findMinimumDeletion($l + 1, $r,
                                      $dp, $s);
 
    // When a group of consecutive characters
    // are deleted if any of them matches
    for ($i = $l + 1; $i <= $r; ++$i)
    {
 
        // When both the characters are same then
        // delete the letters in between them
        if ($s[$l] == $s[$i])
            $res = min($res, findMinimumDeletion($l + 1, $i - 1, $dp, $s) +
                             findMinimumDeletion($i, $r, $dp, $s));
    }
 
    // Memoize
    return $dp[$l][$r] = $res;
}
 
// Driver code
$s = "abcddcba";
$n = strlen($s);
$dp = array(array());
for($i = 0; $i < $GLOBALS['N']; $i++)
    for($j = 0; $j < $GLOBALS['N']; $j++)
        $dp[$i][$j] = -1 ;
         
echo findMinimumDeletion(0, $n - 1, $dp, $s);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
4

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