📌  相关文章
📜  删除给定子字符串要替换的最少字符

📅  最后修改于: 2021-04-29 05:00:22             🧑  作者: Mango

给定两个字符串str1str2 。任务是在字符串str1中找到要用$替换的最小字符数,以使str1不包含字符串str2作为任何子字符串。

例子:

Input: str1 = "intellect", str2 = "tell"
Output: 1
4th character of string "str1" can be replaced by $
such that "int$llect" it does not contain "tell" 
as a substring.

Input: str1 = "google", str2 = "apple"
Output: 0

方法类似于搜索模式|设置1(朴素模式搜索)。
这个想法是找到字符串中的字符串“STR1” STR2赛车’的最左边的发生。如果’str1’的所有字符都与’str2’匹配,我们将替换出现的最后一个符号(或用一个整数增加答案),并增加字符串’str1’的索引,以便它再次检查字符串后的子字符串。替换的字符(即索引i等于i + length(b)-1 )。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// function to calculate minimum
// characters to replace
int replace(string A, string B)
{
  
    int n = A.length(), m = B.length();
    int count = 0, i, j;
  
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
  
            // mismatch occurs
            if (A[i + j] != B[j]) 
                break;
        }
  
        // if all characters matched, i.e,
        // there is a substring of 'a' which
        // is same as string 'b'
        if (j == m) {
            count++;
  
             // increment i to index m-1 such that
            // minimum characters are replaced
            // in 'a'
            i += m - 1;
             
        }
    }
  
    return count;
}
  
// Driver Code
int main()
{
    string str1 = "aaaaaaaa";
    string str2 = "aaa";
  
    cout << replace(str1 , str2);
  
  return 0;
}


Java
// Java implementation of
// above approach
import java.io.*;
  
// function to calculate minimum
// characters to replace
class GFG
{
static int replace(String A, String B)
{
  
    int n = A.length(), m = B.length();
    int count = 0, i, j;
  
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++) 
        {
  
            // mismatch occurs
            if(i + j >= n)
            break;
            else if (A.charAt(i + j) != B.charAt(j)) 
                break;
        }
  
        // if all characters matched, i.e,
        // there is a substring of 'a' which
        // is same as string 'b'
        if (j == m) 
        {
            count++;
  
            // increment i to index m-1 such that
            // minimum characters are replaced
            // in 'a'
            i += m - 1;
              
        }
    }
  
    return count;
}
  
// Driver Code
public static void main(String args[])
{
    String str1 = "aaaaaaaa";
    String str2 = "aaa";
  
    System.out.println(replace(str1 , str2));
}
}
  
// This code is contributed by Subhadeep


Python3
# Python3 implementation of the 
# above approach 
  
# Function to calculate minimum 
# characters to replace 
def replace(A, B): 
  
    n, m = len(A), len(B) 
    count, i = 0, 0
    while i < n: 
          
        j = 0
        while j < m: 
              
            # mismatch occurs 
            if i + j >= n or A[i + j] != B[j]:
                break
              
            j += 1
              
        # If all characters matched, i.e, 
        # there is a substring of 'a' which 
        # is same as string 'b' 
        if j == m:
            count += 1
                  
            # increment i to index m-1 such that 
            # minimum characters are replaced 
            # in 'a' 
            i += m - 1
              
        i += 1
              
    return count 
  
# Driver Code 
if __name__ == "__main__": 
  
    str1 = "aaaaaaaa"
    str2 = "aaa"
  
    print(replace(str1 , str2)) 
  
# This code is contributed by Rituraj Jain


C#
// C# implementation of above approach 
using System;
  
// function to calculate minimum 
// characters to replace 
class GFG
{
public static int replace(string A, 
                          string B)
{
  
    int n = A.Length, m = B.Length;
    int count = 0, i, j;
  
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
  
            // mismatch occurs 
            if (i + j >= n)
            {
            break;
            }
            else if (A[i + j] != B[j])
            {
                break;
            }
        }
  
        // if all characters matched, i.e, 
        // there is a substring of 'a' 
        // which is same as string 'b' 
        if (j == m)
        {
            count++;
  
            // increment i to index m-1 
            // such that minimum characters 
            // are replaced in 'a' 
            i += m - 1;
  
        }
    }
  
    return count;
}
  
// Driver Code 
public static void Main(string[] args)
{
    string str1 = "aaaaaaaa";
    string str2 = "aaa";
  
    Console.WriteLine(replace(str1, str2));
}
}
  
// This code is contributed 
// by Shrikant13


PHP
= $n)
            {
                break;
            }
            else if ($A[$i + $j] != $B[$j])
            {
                break;
            }
        }
  
        // if all characters matched, i.e, 
        // there is a substring of 'a' 
        // which is same as string 'b' 
        if ($j == $m)
        {
            $count++;
  
            // increment i to index m-1 
            // such that minimum characters 
            // are replaced in 'a' 
            $i = $i + $m - 1;
  
        }
    }
  
    return $count;
}
  
// Driver Code 
$str1 = "aaaaaaaa";
$str2 = "aaa";
  
echo (replace($str1, $str2));
  
// This code is contributed
// by Kirti_Mangal
?>


输出:
2

时间复杂度: O(len1 * len2) ,其中len1是第一个字符串的长度,len2是第二个字符串的长度。

另外,可以使用Python的内置函数string1.count(string2)直接解决此问题。

// Python program to find minimum numbers
// of characters to be replaced to
// remove the given substring
str1 = "aaaaaaaa"
str2 = "aaa"
  
# inbuilt function
answer = str1.count(str2)
print(answer)
输出:
2