How to check whether second string can be formed from characters of first string in Python

1 Answer

0 votes
from collections import defaultdict
 
def second_string_can_be_formed_from_first_string(first, second):
    freqency = defaultdict(int)
 
    for i in range(len(first)):
        freqency[first[i]] += 1

    for i in range(len(second)):
        if freqency[second[i]] == 0:
            return False
 
    return True

first = "apyoknthtm"
second = "python"

print(second_string_can_be_formed_from_first_string(first, second))



'''
run:

True

'''

 



answered Mar 23, 2024 by avibootz
...