Count all substrings having character K
Given a string str and a character K, the task is to find the count of all the substrings of str that contain the character K.
Examples:
Input: str = “geeks”, K = ‘g’
Output: 5
“g”, “ge”, “gee”, “geek” and “geeks” are the valid substrings.
Input: str = “geeksforgeeks”, K = ‘k’
Output: 56
Naive approach A simple approach will be to find all the substrings having character K of the given string and return the count;
Efficient approach: For every index i in the string, find the first index j such that i ≤ j and str[j] = K. Now, the substrings str[i…j], str[i…j + 1], str[i…j + 2], …, str[i…n – 1] will all contain the character K at least once. The approach seems to be O(n2) at first but the index j will not be calculated again for every index i, j will be a valid index for all the values of i less than j.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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 substrings of strr which contain # the character ch at least one def countSubStr(strr, n, ch): # To store the count of valid substrings 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 # 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 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 |
Javascript
<script> // JavaScript implementation of the approach // Function to return the index of the // next occurrence of character ch in str // starting from the given index function nextOccurrence(str, n, start, ch) { for ( var 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 function countSubStr(str, n, ch) { // To store the count of valid substrings var cnt = 0; // Index of the first occurrence of ch in str var j = nextOccurrence(str, n, 0, ch); for ( var 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 var str = "geeksforgeeks" ; var n = str.length; var ch = "k" ; document.write(countSubStr(str, n, ch)); </script> |
56
Please Login to comment...