Given a string str, the task is to count all possible sub-strings of the given string that are prefix of the same string.
Input: str = “ababc”
All possible sub-string are “a”, “ab”, “aba”, “abab”, “ababc”, “a” and “ab”
Input: str = “abdabc”
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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Longest string in an array which matches with prefix of the given string
- Print the longest prefix of the given string which is also the suffix of the same string
- Longest string which is prefix string of at least two strings
- Replace two substrings (of a string) with each other
- Number of substrings of a string
- Strings from an array which are not prefix of any other string
- String from prefix and suffix of given two strings
- Program to print all substrings of a given string
- Number of substrings of one string present in other
- Different substrings in a string that start and end with given strings
- Split the string into substrings using delimiter
- Sum of all substrings of a string representing a number | Set 1
- Lexicographical concatenation of all substrings of a string
- Count of substrings of a binary string containing K ones
- Number of even substrings in a string of digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.