Sub-strings of a string that are prefix of the same string
Given a string str, the task is to count all possible sub-strings of the given string that are prefix of the same string.
Examples:
Input: str = “ababc”
Output: 7
All possible sub-string are “a”, “ab”, “aba”, “abab”, “ababc”, “a” and “ab”
Input: str = “abdabc”
Output: 8
Approach: Traverse the string character by character, if the current character is equal to the first character of the string then count all possible sub-strings starting from here that are also the prefixes of str and add it to count. After the complete string has been traversed, print the count.
Below is the implementation of the above approach:
Java
// Java implementation of the approach public class GFG { // Function to return the count of sub-strings starting // from startIndex that are also the prefixes of str public static int subStringsStartingHere(String str, int n, int startIndex) { int count = 0, i = startIndex + 1; while (i <= n) { if (str.startsWith(str.substring(startIndex, i))) { count++; } else break; i++; } return count; } // Function to return the count of all possible sub-strings // of str that are also the prefixes of str public static int countSubStrings(String str, int n) { int count = 0; for (int i = 0; i < n; i++) { // If current character is equal to // the starting character of str if (str.charAt(i) == str.charAt(0)) count += subStringsStartingHere(str, n, i); } return count; } // Driver code public static void main(String[] args) { String str = "ababc"; int n = str.length(); System.out.println(countSubStrings(str, n)); } } |
Python3
# Python3 implementation of the approach # Function to return the count of sub-strings starting # from startIndex that are also the prefixes of string def subStringsStartingHere(string, n, startIndex) : count = 0 i = startIndex + 1 while(i <= n) : if string.startswith(string[startIndex : i]) : count += 1 else : break i += 1 return count # Function to return the count of all possible sub-strings # of string that are also the prefixes of string def countSubStrings(string, n) : count = 0 for i in range(n) : # If current character is equal to # the starting character of str if string[i] == string[0] : count += subStringsStartingHere(string, n, i) return count # Driver Code if __name__ == "__main__" : string = "ababc" n = len(string) print(countSubStrings(string, n)) # this code is contributed by Ryuga |
C#
// C# implementation of the approach using System; class GFG { // Function to return the count of sub-strings starting // from startIndex that are also the prefixes of str static int subStringsStartingHere(String str, int n, int startIndex) { int count = 0, i = startIndex + 1; while (i <= n) { if (str.StartsWith(str.Substring(startIndex, i-startIndex))) { count++; } else break; i++; } return count; } // Function to return the count of all possible sub-strings // of str that are also the prefixes of str static int countSubStrings(String str, int n) { int count = 0; for (int i = 0; i < n; i++) { // If current character is equal to // the starting character of str if (str[i] == str[0]) count += subStringsStartingHere(str, n, i); } return count; } // Driver code static public void Main(String []args) { String str = "ababc"; int n = str.Length; Console.WriteLine(countSubStrings(str, n)); } } //contributed by Arnab Kundu |
7
Recommended Posts:
- Python program to print the substrings that are prefix of the given string
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Print the longest prefix of the given string which is also the suffix of the same string
- Longest string in an array which matches with prefix of the given string
- Replace two substrings (of a string) with each other
- Number of substrings of a string
- Lexicographical concatenation of all substrings of a string
- Number of substrings of one string present in other
- Split the string into substrings using delimiter
- Program to print all substrings of a given string
- Different substrings in a string that start and end with given strings
- Sum of all substrings of a string representing a number | Set 1
- Number of even substrings in a string of digits
- Count of substrings of a binary string containing K ones
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



