Python - Test substring order
Last Updated :
27 Apr, 2023
Given two strings, check if substring characters occur in correct order in string.
Input : test_str = 'geeksforgeeks', K = 'sees' Output : True Explanation : "s" after that "ee" and then "s" is present in order in string 1.
Input : test_str = 'geeksforgeeks', K = 'seef' Output : False Explanation : Unordered String.
Method #1 : Using join() + generator expression + in operator
In this, we check we join all the characters which occur in the substring using join(), post that check if substring is present using in operator.
Python3
# Python3 code to demonstrate working of
# Test substring order
# Using join() + in operator + generator expression
# initializing string
test_str = 'geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing substring
K = 'seek'
# concatenating required characters
def temp(sub): return ''.join(chr for chr in sub if chr in set(K))
# checking in order
res = K in temp(test_str)
# printing result
print("Is substring in order : " + str(res))
OutputThe original string is : geeksforgeeks
Is substring in order : True
Time Complexity: O(n)
Space Auxiliary: O(n)
Method #2 : Using all() + next() + generator expression
In this, we get the string with just substring characters using next() and generator expression, to check for order, all() operation is used for each character in substring.
Python3
# Python3 code to demonstrate working of
# Test substring order
# Using all() + next() + generator expression
# initializing string
test_str = 'geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing substring
K = 'seek'
# concatenating required characters using next()
# all() used to test order
test_str = iter(test_str)
res = all(next((ele for ele in test_str if ele == chr), None) is not None for chr in K)
# printing result
print("Is substring in order : " + str(res))
OutputThe original string is : geeksforgeeks
Is substring in order : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using find() method
Python3
# Python3 code to demonstrate working of
# Test substring order
# initializing string
test_str = 'geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing substring
K = 'seek'
ns = ""
for i in test_str:
if i in K:
ns += i
res = False
if(ns.find(K) != -1):
res = True
# printing result
print("Is substring in order : " + str(res))
OutputThe original string is : geeksforgeeks
Is substring in order : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using replace() and find() methods
Python3
# Python3 code to demonstrate working of
# Test substring order
# initializing string
test_str = 'geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing substring
K = 'seek'
for i in test_str:
if i not in K:
test_str=test_str.replace(i,"")
res=False
if(test_str.find(K)!=-1):
res=True
# printing result
print("Is substring in order : " + str(res))
OutputThe original string is : geeksforgeeks
Is substring in order : True
Method #5 : Using operator.contains() method
Approach
- Replace all characters of test_str which are not present in K using replace() method
- Now check whether K is present in test_str using operator.contains() method
- Display the return value of operator.contains()
Python3
# Python3 code to demonstrate working of
# Test substring order
# initializing string
test_str = 'geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing substring
K = 'seek'
for i in test_str:
if i not in K:
test_str=test_str.replace(i,"")
import operator
res=operator.contains(test_str,K)
# printing result
print("Is substring in order : " + str(res))
OutputThe original string is : geeksforgeeks
Is substring in order : True
Time Complexity : O(N) N- length of string
Auxiliary Space - O(1) because res is a single variable
Method 6 : using list comprehension and the all() function
Python3
# Python3 code to demonstrate working of
# Test substring order
# initializing string
test_str = 'geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing substring
K = 'seek'
# using list comprehension and all()
res = all(K[i] in test_str[test_str.find(K[i-1])+1:] for i in range(1, len(K)))
# printing result
print("Is substring in order : " + str(res))
OutputThe original string is : geeksforgeeks
Is substring in order : True
The time complexity of this approach is O(n^2), where n is the length of the substring K, because for each character in K, we need to search through the remaining part of test_str to find its position.
The auxiliary space complexity is O(1), because we only use a few extra variables to store the current position in test_str, the previous position found by find(), and the result of the all() function. No additional data structures are created.
Similar Reads
Python - Test for desired String Lengths Given a String list, check for all string if it matches the desired string length from 2nd list of sizes. Input : test_list = ["Gfg", 'for', 'geeks'], len_list = [3, 3, 5] Output : True Explanation : All are of desired lengths. Input : test_list = ["Gfg", 'for', 'geek'], len_list = [3, 3, 5] Output
6 min read
Find the Index of a Substring in Python Finding the position of a substring within a string is a common task in Python. In this article, we will explore some simple and commonly used methods to find the index of a substring in Python.Using str.find() The find() method searches for the first occurrence of the specified substring and return
3 min read
Python Extract Substring Using Regex Python provides a powerful and flexible module called re for working with regular expressions. Regular expressions (regex) are a sequence of characters that define a search pattern, and they can be incredibly useful for extracting substrings from strings. In this article, we'll explore four simple a
2 min read
Python Program to Sort a String Sorting strings in Python is a common and important task, whether we need to organize letters alphabetically or systematically handle text data. In this article, we will explore different methods to sort a string starting from the most efficient to the least.Using sorted with join()sorted() function
2 min read
How to Get Index of a Substring in Python? To get index of a substring within a Python string can be done using several methods such as str.find(), str.index(), and even regular expressions. Each approach has its own use case depending on the requirements. Letâs explore how to efficiently get the index of a substring.The simplest way to get
2 min read
Python - String till Substring When working with Python strings, we may encounter a situation where we need to extract a portion of a string that starts from the beginning and stops just before a specific substring. Let's discuss certain ways in which we can do this.Using split()The split() method is a simple and efficient way to
3 min read
How to Find Index of a Substring in Python In Python, sometimes we need to know where a certain substring appears in a string. To do this, we can find the index (or position) of that substring. The index tells us where the substring is located. Letâs look at some simple ways to find the index of a substring in a string.Using find() methodThe
2 min read
Python - Extract Sorted Strings Given a String List, extract all sorted strings. Input : test_list = ["hint", "geeks", "fins", "Gfg"] Output : ['hint', 'fins', 'Gfg'] Explanation : Strings in increasing order of characters are extracted.Input : test_list = ["hint", "geeks", "Gfg"] Output : ['hint', 'Gfg'] Explanation : Strings in
5 min read
Python | K Character Split String The problems and at the same time applications of list splitting is quite common while working with python strings. Some characters are usually tend to ignore in the use cases. But sometimes, we might not need to omit those characters but include them in our programming output. Letâs discuss certain
4 min read
Python - Word starting at Index Sometimes, while working with Python, we can have a problem in which we need to extract the word which is starting from a particular index. This can have a lot of applications in school programming. Let's discuss certain ways in which this task can be performed. Method #1: Using a loop This is a bru
2 min read