Given a string str, the task is to check whether the string contains two non-overlapping sub-strings s1 = “geek” and s2 = “keeg” such that s2 starts after s1 ends.
Input: str = “geekeekeeg”
“geek” and “keeg” both are present in the
given string without overlapping.
Input: str = “geekeeg”
“geek” and “keeg” both are present but they overlap.
Approach: Check if the sub-string “geek” occurs before “keeg” in the given string. This problem is simpler when we use a predefined function strstr in order to find the occurrence of a sub-string in the given string.
Below is the implementation of the above approach:
# Python 3 implementation of the approach
# Function that returns true
# if s contains two non overlapping
# sub strings “geek” and “keeg”
# If “geek” and “keeg” are both present
# in s without over-lapping and “keeg”
# starts after “geek” ends
# Driver code
if __name__ == “__main__”:
s = “geekeekeeg”
# This code is contributed by ChitraNayal
- Count of non-overlapping sub-strings "101" and "010" in the given binary string
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Replace two substrings (of a string) with each other
- Number of substrings of a string
- Program to print all substrings of a given string
- Lexicographical concatenation of all substrings of a string
- Sum of all substrings of a string representing a number | Set 1
- Number of even substrings in a string of digits
- Different substrings in a string that start and end with given strings
- Number of substrings of one string present in other
- Count of substrings of a binary string containing K ones
- Split the string into substrings using delimiter
- Number of substrings with odd decimal value in a binary string
- Minimum changes to a string to make all substrings distinct
- Number of substrings divisible by 6 in a string of integers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.