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.

1 st = 'hello'
2 ar = 'el'
3 print(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.

1 st = set('hello')
2 ar = 'el'
3 print(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:

string contains all chars ordered