Test if String is Subset of Another – Python
We are given two strings s1
and s2 and the task is to
check whether s2
is a subsequence of s1
. A subsequence is a sequence that appears in the same relative order but not necessarily consecutively.
Using all()
In this method we check whether all the characters of s2
are present in s1
by using the all()
function and it returns True
if all the conditions in the iterable are True
.
s1 = "geeksforgeeks"
s2 = "gfks"
# Check if all characters of s2 are present in s1
res = all(ele in s1 for ele in s2)
print(res)
s1 = "geeksforgeeks"
s2 = "gfks"
# Check if all characters of s2 are present in s1
res = all(ele in s1 for ele in s2)
print(res)
Output
True
Let’s check other methods to achieve the same :
Table of Content
Using issubset()
In this method we convert s2
into a set then check if all characters in s2
are a subset of s1.then
utilize the issubset()
method of sets to directly determine the inclusion.
s1 = "geeksforgeeks"
s2 = "gfks"
# Check if s2 is a subset of s1
res = set(s2).issubset(s1)
print(res)
s1 = "geeksforgeeks"
s2 = "gfks"
# Check if s2 is a subset of s1
res = set(s2).issubset(s1)
print(res)
Output
True
Using a Recursive Method
In this method we recursively check if each character of s2
exists in s1
in order and reduce both strings step by step in order to do that.
def is_subset(s1, s2):
if not s2:
return True
if not s1:
return False
if s1[0] == s2[0]:
return is_subset(s1[1:], s2[1:])
return is_subset(s1[1:], s2)
s1 = "geeksforgeeks"
s2 = "gfks"
res = is_subset(s1, s2)
print(res)
def is_subset(s1, s2):
if not s2:
return True
if not s1:
return False
if s1[0] == s2[0]:
return is_subset(s1[1:], s2[1:])
return is_subset(s1[1:], s2)
s1 = "geeksforgeeks"
s2 = "gfks"
res = is_subset(s1, s2)
print(res)
Output
True
Explanation:
- function is_subset() checks character by character if
s2
is a subsequence ofs1
recursively. - If
s2
becomes empty it means all characters were found ins1
and we returnTrue o
therwise false
is returned.
Using the filter()
function and len()
This method uses filter()
along with lambda
to create a filtered list of characters from s2
that are also in s1
, and then checks if the length of this list matches the length of s2
.
s1 = "geeksforgeeks"
s2 = "gfks"
# Filter characters from s2 that are in s1, then check if their count equals s2's length
result = len(s2) == len(list(filter(lambda c: c in s1, s2)))
print(result) # Output: True
s1 = "geeksforgeeks"
s2 = "gfks"
# Filter characters from s2 that are in s1, then check if their count equals s2's length
result = len(s2) == len(list(filter(lambda c: c in s1, s2)))
print(result) # Output: True
Output
True
Explanation: filter()
function checks each character in s2
to see if it is present in s1
and the len()
function verifies if the number of matching characters is the same as the length of s2
.
Using the Counter
function
This method utilizes the Counter
class from the collections
module to count the frequency of characters in both strings and then checks if all characters of s2
are present in s1
.
from collections import Counter
s1 = "geeksforgeeks"
s2 = "gfks"
# Count characters in both strings
count1 = Counter(s1)
count2 = Counter(s2)
# Check if all characters of s2 are in s1
result = not bool(count2 - count1)
print(result)
from collections import Counter
s1 = "geeksforgeeks"
s2 = "gfks"
# Count characters in both strings
count1 = Counter(s1)
count2 = Counter(s2)
# Check if all characters of s2 are in s1
result = not bool(count2 - count1)
print(result)
Output
True
Explanation:
Counter
class counts the frequency of characters in boths1
ands2 and this
creates dictionaries where each key is a character and the value is its frequency in the respective string.- Expression
count2 - count1
checks if any character ins2
has a greater frequency than ins1 and i
f the result is an empty dictionary it meanss2
is a subsequence ofs1
and hence True
is returned.