Given a string str consisting of lowercase English alphabets, the task is to find the count of substrings that start with “neveropen” and end with “for”.
Examples:
Input: str = “neveropenisforneveropen”
Output: 3
“neveropenfor”, “neveropenisfor” and “neveropenisfor”
are the only valid substrings.Input: str = “neveropen”
Output: 1
Naive approach: First set the counter to 0 then iterate over the string and whenever the substring “neveropen” is encountered, from the very next indices again iterate over the string and try to find the substring “for”. If “for” is present then increment the counter and finally print it.
Efficient approach: Set two counter for the substrings “neveropen” and “for”, say c1 and c2. On iterating, whenever the substring “neveropen” is encountered, increment c1 and whenever “for” is encountered, set c2 = c2 + c1. This is because every occurrence of “neveropen” will make a valid substring with the current found “for”. Finally, print c2.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to return the count // of required substrings int countSubStr(string s, int n) { int c1 = 0, c2 = 0; // For every index of the string for ( int i = 0; i < n; i++) { // If the substring starting at // the current index is "neveropen" if (s.substr(i, 5) == "neveropen" ) c1++; // If the substring is "for" if (s.substr(i, 3) == "for" ) c2 = c2 + c1; } return c2; } // Driver code int main() { string s = "neveropenisforneveropen" ; int n = s.size(); cout << countSubStr(s, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the count // of required substrings static int countSubStr(String s, int n) { int c1 = 0 , c2 = 0 ; // For every index of the string for ( int i = 0 ; i < n; i++) { // If the substring starting at // the current index is "neveropen" if (i < n - 5 && "neveropen" .equals(s.substring(i, i + 5 ))) { c1++; } // If the substring is "for" if (i < n - 3 && "for" .equals(s.substring(i, i + 3 ))) { c2 = c2 + c1; } } return c2; } // Driver code public static void main(String[] args) { String s = "neveropenisforneveropen" ; int n = s.length(); System.out.println(countSubStr(s, n)); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach # Function to return the count # of required substrings def countSubStr(s, n) : c1 = 0 ; c2 = 0 ; # For every index of the string for i in range (n) : # If the substring starting at # the current index is "neveropen" if (s[i : i + 5 ] = = "neveropen" ) : c1 + = 1 ; # If the substring is "for" if (s[i :i + 3 ] = = "for" ) : c2 = c2 + c1; return c2; # Driver code if __name__ = = "__main__" : s = "neveropenisforneveropen" ; n = len (s); print (countSubStr(s, n)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; public class GFG { // Function to return the count // of required substrings static int countSubStr(String s, int n) { int c1 = 0, c2 = 0; // For every index of the string for ( int i = 0; i < n; i++) { // If the substring starting at // the current index is "neveropen" if (i < n - 5 && "neveropen" .Equals(s.Substring(i, 5))) { c1++; } // If the substring is "for" if (i < n - 3 && "for" .Equals(s.Substring(i, 3))) { c2 = c2 + c1; } } return c2; } // Driver code public static void Main(String[] args) { String s = "neveropenisforneveropen" ; int n = s.Length; Console.WriteLine(countSubStr(s, n)); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // JavaScript implementation of the approach // Function to return the count // of required substrings function countSubStr(s, n) { var c1 = 0, c2 = 0; // For every index of the string for ( var i = 0; i < n; i++) { // If the substring starting at // the current index is "neveropen" if (s.substring(i, i+5) == "neveropen" ) c1++; // If the substring is "for" if (s.substring(i,i+ 3) == "for" ) c2 = c2 + c1; } return c2; } // Driver code var s = "neveropenisforneveropen" ; var n = s.length; document.write( countSubStr(s, n)); </script> |
3
Time Complexity: O(N), where N is the length of the given string
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!