📌  相关文章
📜  计算所有具有字符K的子字符串

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

给定一个字符串str和一个字符K ,任务是查找包含字符Kstr的所有子字符串的计数。

例子:

天真的方法一种简单的方法是找到所有具有给定字符串的字符K的子字符串并返回计数。

高效的方法:对于字符串中的每个索引i ,找到第一个索引j ,以使i≤jstr [j] = K。现在,子字符串str [i…j],str [i…j + 1],str [i…j + 2],…,str [i…n – 1]将全部包含字符K至少一次。这种方法似乎是O(N 2)在第一,但指标j不会为每个索引再次计算,J将是所有小于j中的i值的一个有效指标。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the index of the
// next occurrence of character ch in str
// starting from the given index
int nextOccurrence(string str, int n,
                   int start, char ch)
{
    for (int i = start; i < n; i++) {
  
        // Return the index of the first
        // occurrence of ch
        if (str[i] == ch)
            return i;
    }
  
    // No occurrence found
    return -1;
}
  
// Function to return the count of all
// the substrings of str which contain
// the character ch at least one
int countSubStr(string str, int n, char ch)
{
  
    // To store the count of valid substrings
    int cnt = 0;
  
    // Index of the first occurrence of ch in str
    int j = nextOccurrence(str, n, 0, ch);
    for (int i = 0; i < n; i++) {
        while (j != -1 && j < i) {
            j = nextOccurrence(str, n, j + 1, ch);
        }
  
        // No occurrence of ch after index i in str
        if (j == -1)
            break;
  
        // Substrings starting at index i
        // and ending at indices j, j+1, ..., n-1
        // are all valid substring
        cnt += (n - j);
    }
  
    return cnt;
}
  
// Driver code
int main()
{
  
    string str = "geeksforgeeks";
    int n = str.length();
    char ch = 'k';
  
    cout << countSubStr(str, n, ch);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
      
    // Function to return the index of the 
    // next occurrence of character ch in str 
    // starting from the given index 
    static int nextOccurrence(String str, int n, 
                              int start, char ch) 
    { 
        for (int i = start; i < n; i++) 
        { 
      
            // Return the index of the first 
            // occurrence of ch 
            if (str.charAt(i) == ch) 
                return i; 
        } 
      
        // No occurrence found 
        return -1; 
    } 
      
    // Function to return the count of all 
    // the substrings of str which contain 
    // the character ch at least one 
    static int countSubStr(String str, 
                           int n, char ch) 
    { 
      
        // To store the count of valid substrings 
        int cnt = 0; 
      
        // Index of the first occurrence of ch in str 
        int j = nextOccurrence(str, n, 0, ch); 
        for (int i = 0; i < n; i++) 
        { 
            while (j != -1 && j < i)
            { 
                j = nextOccurrence(str, n, j + 1, ch); 
            } 
      
            // No occurrence of ch after index i in str 
            if (j == -1) 
                break; 
      
            // Substrings starting at index i 
            // and ending at indices j, j+1, ..., n-1 
            // are all valid substring 
            cnt += (n - j); 
        } 
        return cnt; 
    } 
      
    // Driver code 
    static public void main ( String []arg)
    { 
        String str = "geeksforgeeks"; 
        int n = str.length(); 
        char ch = 'k'; 
          
        System.out.println(countSubStr(str, n, ch)); 
    } 
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
  
# Function to return the index of the
# next occurrence of character ch in strr
# starting from the given index
def nextOccurrence(strr, n, start, ch):
    for i in range(start, n):
  
        # Return the index of the first
        # occurrence of ch
        if (strr[i] == ch):
            return i
  
    # No occurrence found
    return -1
  
# Function to return the count of all
# the substrrings of strr which contain
# the character ch at least one
def countSubStr(strr, n, ch):
  
    # To store the count of valid substrrings
    cnt = 0
  
    # Index of the first occurrence of ch in strr
    j = nextOccurrence(strr, n, 0, ch)
  
    for i in range(n):
        while (j != -1 and j < i):
            j = nextOccurrence(strr, n, j + 1, ch)
  
        # No occurrence of ch after index i in strr
        if (j == -1):
            break
  
        # Substrrings starting at index i
        # and ending at indices j, j+1, ..., n-1
        # are all valid substrring
        cnt += (n - j)
  
    return cnt
  
# Driver code
strr = "geeksforgeeks"
n = len(strr)
ch = 'k'
  
print(countSubStr(strr, n, ch))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach 
using System;
  
class GFG
{
      
    // Function to return the index of the 
    // next occurrence of character ch in str 
    // starting from the given index 
    static int nextOccurrence(String str, int n, 
                               int start, char ch) 
    { 
        for (int i = start; i < n; i++) 
        { 
      
            // Return the index of the first 
            // occurrence of ch 
            if (str[i] == ch) 
                return i; 
        } 
      
        // No occurrence found 
        return -1; 
    } 
      
    // Function to return the count of all 
    // the substrings of str which contain 
    // the character ch at least one 
    static int countSubStr(String str, 
                           int n, char ch) 
    { 
      
        // To store the count of valid substrings 
        int cnt = 0; 
      
        // Index of the first occurrence of ch in str 
        int j = nextOccurrence(str, n, 0, ch); 
        for (int i = 0; i < n; i++) 
        { 
            while (j != -1 && j < i)
            { 
                j = nextOccurrence(str, n, j + 1, ch); 
            } 
      
            // No occurrence of ch after index i in str 
            if (j == -1) 
                break; 
      
            // Substrings starting at index i 
            // and ending at indices j, j+1, ..., n-1 
            // are all valid substring 
            cnt += (n - j); 
        } 
        return cnt; 
    } 
      
    // Driver code 
    static public void Main () 
    { 
        String str = "geeksforgeeks"; 
        int n = str.Length; 
        char ch = 'k'; 
          
        Console.WriteLine(countSubStr(str, n, ch)); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
56