If I have string "axplpett" I would like to return true, because a substring of it "axplpe" contains all of the letters of apple.
I was thinking of using the set method, but apple has repeating characters.
If I have string "axplpett" I would like to return true, because a substring of it "axplpe" contains all of the letters of apple.
I was thinking of using the set method, but apple has repeating characters.
r = 'aapple'
w = list('axplpett')
try:
for x in r:
w.pop(w.index(x))
print(True) # return True
except ValueError:
print(False) # return False
Simply this?
string = "axplpett"
test = "apple"
all(string.count(l) >= test.count(l) for l in test)
You can easily do this with a Counter.
We subtract the count of letters in the target string 'apple' from the count of letters in the test string 'axplpett'. Any letters in the target string that aren't in the test string will result in negative counts for those letters. We then negate that result, which strips off positive or zero counts, the resulting counter will be empty if the target string is contained within the test string.
from collections import Counter
target = 'apple'
test = 'axplpett'
counts = Counter(test)
counts.subtract(target)
print(not -counts)
counts = Counter('axplett')
counts.subtract(target)
print(not -counts)
output
True
False
Maybe this:-
def findsub (sub,main):
l1 = [i for i in sub]
l2 = [i for i in main]
for item in l1:
if item in l2:
l2.remove(item)
else:
return False
return True
print(findsub('apple','appleccssfftt'))