string contains all chars unordered
There are various approaches to help us ascertain whether all characters in ar
appear in the string st
.
time O(n^2), space O(1)
The first is to use a generator expression
that checks the existance using the in
keyword, and being input into all
.
1st = 'hello'
2ar = 'el'
3print(all(c in st for c in ar))
time O(n), space O(n)
Trading off some space for some time, we can convert our string to a set. This is probably a far better solution in practice.
1st = set('hello')
2ar = 'el'
3print(all(c in st for c in ar))
This takes N
of s
to build the set, and N
of el
for the lookups. So
See also: