VOOZH about

URL: https://www.geeksforgeeks.org/python/python-test-if-substring-occurs-in-specific-position/

⇱ Python - Test if Substring occurs in specific position - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Test if Substring occurs in specific position

Last Updated : 8 Apr, 2023

Sometimes, while working with python strings, we can have a problem in which we need to test occurrence of substring. There is straight forward way to test this. But sometimes, we have a more specific problem in which we need to test if substring occurs at that particular position. Let's discuss certain ways in which this task can be performed. 

Method #1: Using loop This is brute method to solve this problem. In this we iterate the string and when index occur we test for substring characters simultaneously. 

Output : 
The original string is : Gfg is best
Does string contain substring at required position ? : True

Time Complexity: O(n) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(1), constant extra space is needed

Method #2: Using string slicing This is a one-liner way to perform this task. In this, we check the substring in string using string slicing. 

Output : 
The original string is : Gfg is best
Does string contain substring at required position ? : True

Method #3 : Using find() method


Output
The original string is : Gfg is best
Does string contain substring at required position ? : True

Method#4: Using Recursive method.

Algorithm:

  1. Check if the ending index j is greater than the length of the input string test_str. If it is, return False, because the substring cannot exist outside the range of the input string.
  2. Check if the substring starting from index i and ending at index j is equal to the input substring substr. If it is, return True, because the substring has been found at the specified position.
  3. Otherwise, call the function recursively with the starting index incremented by 1 and the ending index incremented by 1, to check the next possible substring.

Output
The original string is : Gfg is best
Does string contain substring at required position? True

The time complexity of this function is O(n), where n is the length of the input string test_str. This is because in the worst case, the function will need to iterate over the entire string to find the substring.

The auxiliary space complexity of the function is O(1), because it does not use any extra space that is proportional to the size of the input. The function only uses a constant amount of space to store the input parameters and temporary variables.

Method #5: Using Regular Expression

Step-by-step approach:

  • Import the re module.
  • Define a regular expression pattern that matches the substring at the specific position in the string. The pattern should use the "^" character to anchor the search at the specific position and the "$" character to match the end of the string.
  • Use the re.search() function to search for the regular expression pattern in the string.
  • If a match is found, set the result to True, else set it to False.
  • Print the result.

Output
The original string is : Gfg is best
Does string contain substring at required position ? : True

The time complexity of this program is O(1) because it uses a constant amount of time to compile the regular expression pattern and search for the pattern in the string.

The auxiliary space of this program is also O(1) because it uses a constant amount of memory to store the regular expression pattern and the result of the search operation.

Comment