Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Test if string is subset of another

Python | Test if string is subset of another

Sometimes, while working with strings, we can have a problem in which we need to test if a string is a subsequence of another. This can have possible application in many domains including data science and day-day programming. Lets discuss certain ways in which this task can be performed. 

Method #1: Using all() This is one of the by which we can solve this problem. In this, we employ all() to check if all the characters of one string are present in another. 

Python3




# Python3 code to demonstrate working of
# Test if string is subset of another
# Using all()
 
# initializing strings
test_str1 = "neveropen"
test_str2 = "gfks"
 
# printing original string
print("The original string is : " + test_str1)
 
# Test if string is subset of another
# Using all()
res = all(ele in test_str1 for ele in test_str2)
 
# printing result
print("Does string contains all the characters of other list? : " + str(res))


Output : 

The original string is : neveropen
Does string contains all the characters of other list? : True

Time complexity: O(m*n), where m is the length of the first string and n is the length of the second string.
Auxiliary space: O(1), because it only uses a few variables (test_str1, test_str2, res, and ele) that do not depend on the size of the input strings. 

Method #2: Using issubset() Using an inbuilt function is one of the ways in which this task can be performed. In this, we just employ the function and it returns the result after internal processing. 

Python3




# Python3 code to demonstrate working of
# Test if string is subset of another
# Using issubset()
 
# initializing strings
test_str1 = "neveropen"
test_str2 = "gfks"
 
# printing original string
print("The original string is : " + test_str1)
 
# Test if string is subset of another
# Using issubset()
res = set(test_str2).issubset(test_str1)
 
# printing result
print("Does string contains all the characters of other list? : " + str(res))


Output : 

The original string is : neveropen
Does string contains all the characters of other list? : True

Time Complexity: O(n)
Auxiliary Space: O(n)

Method#3: Using Recursive method.

  • Initialize an empty set called set1
  • Loop through each character in test_str1
    • Add the character to set1
  • Loop through each character in test_str2
    • If the character is not in set1, return False
    • If all characters in test_str2 are in set1, return True

Python3




def is_subset(test_str1, test_str2):
    if not test_str2:
        return True
    if not test_str1:
        return False
    if test_str1[0] == test_str2[0]:
        return is_subset(test_str1[1:], test_str2[1:])
    return is_subset(test_str1[1:], test_str2)
 
 
# initializing strings
test_str1 = "neveropen"
test_str2 = "gfks"
 
# printing original string
print("The original string is : " + test_str1)
 
# Test if string is subset of another
# Using recursive function
res = is_subset(test_str1, test_str2)
 
# printing result
print("Does string contains all the characters of other list? : " + str(res))
# this code contributed by tvsk


Output

The original string is : neveropen
Does string contains all the characters of other list? : True

Time Complexity: O(m*n), where m and n are the lengths of the input strings test_str1 and test_str2, respectively. This is because the function recursively checks each character of test_str2 against each character of test_str1, and in the worst case, all characters of both strings will be checked.
Auxiliary Space: O(m*n), as each recursive call creates a new slice of the input strings. Therefore, if the input strings are very long, the function will use a lot of memory due to the recursive calls.

Method#4:  Using the ‘filter’ function and the ‘len’ function: 

  • Initialize the strings test_str1 and test_str2.
  • Print the original value of test_str1.
  • Use filter() and lambda to create a new list that only contains characters from test_str2 that are also in test_str1.
  • Convert the filtered list to a normal list and check if its length is equal to the length of test_str2.
  • Print the result.

Python3




# Python3 code to demonstrate working of
# Test if string is subset of another
# Using filter() and lambda
 
# initializing strings
test_str1 = "neveropen"
test_str2 = "gfks"
 
# printing original string
print("The original string is : " + test_str1)
 
# Test if string is subset of another
# Using filter() and lambda
 
# Use filter() and lambda to generate a new list of characters from test_str2
# that are also in test_str1, and then check if the length of that list
# is equal to the length of test_str2
result = len(test_str2) == len(list(filter(lambda c: c in test_str1, test_str2)))
 
# printing result
print("Does string contains all the characters of other list? : " + str(result))
#This code is contributed by Jyothi pinjala


Output

The original string is : neveropen
Does string contains all the characters of other list? : True

Time complexity : O(n), where n is the length of the shorter string between test_str1 and test_str2. This is because the filter() function needs to iterate through test_str2 to check if each character is also in test_str1.
Auxiliary space : O(n), where n is the length of the shorter string between test_str1 and test_str2. This is because the filter() function creates a new list containing at most n characters, and the list() function also creates a new list containing those n characters. The len() function and boolean comparison also take constant space.

Method#5: Using Counter function

Step-by-step approach:

  • Initialize two strings test_str1 and test_str2.
  • Print the original string.
  • Use all() method to test if string test_str2 is a subset of string test_str1.
  • Store the result in the variable res.
  • Print the final result.

Python3




# importing Counter from collections module
from collections import Counter
 
# initializing strings
test_str1 = "neveropen"
test_str2 = "gfks"
 
# printing original string
print("The original string is : " + test_str1)
 
# Test if string is subset of another
# Using Counter() function
count1 = Counter(test_str1)
count2 = Counter(test_str2)
res = not bool(count2 - count1)
 
# printing result
print("Does string contains all the characters of other list? : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The original string is : neveropen
Does string contains all the characters of other list? : True

Time Complexity: O(n*m), where n is the length of the string test_str1 and m is the length of the string test_str2. This is because we need to check if each character of test_str2 is present in test_str1.
Auxiliary Space: O(1), as we are not using any additional data structures and are only storing boolean values in a single variable res.

Method#6: Using reduce() function with lambda function:

Algorithm:

  1. Import the reduce function from functools module.
  2. Define the is_subset function that takes two string arguments, str1 and str2.
  3. In the is_subset function, use the reduce function to apply a logical AND operation to all the elements of the sequence.
  4. Use the map function to apply a lambda function that checks if each character in str2 is in str1.
  5. Return the result of reduce and map as the output of the function.
  6. Define the test_str1 and test_str2 strings.
  7. Call the is_subset function and pass test_str1 and test_str2 as arguments.
  8. Print the result.

Python3




from functools import reduce
 
def is_subset(str1, str2):
  return reduce(lambda a, b: a and b, map(lambda c: c in str1, str2))
 
# Example usage:
test_str1 = "neveropen"
test_str2 = "gfks"
# printing original string
print("The original string is : " + test_str1)
print(is_subset(test_str1, test_str2))
#This code is contributed by Rayudu


Output

The original string is : neveropen
True

Time complexity:
The time complexity of the is_subset function is O(n), where n is the length of str2. The map function takes O(n) time to create a new list of Boolean values, and the reduce function takes O(n) time to compute the final result.

Space complexity:
The space complexity of the is_subset function is also O(n), where n is the length of str2. The map function creates a new list of Boolean values, which requires O(n) space, and the reduce function requires O(1) space.
 

RELATED ARTICLES

Most Popular

Recent Comments