How to check whether two strings contain same characters in same order in Python

1 Answer

0 votes
from collections import OrderedDict
  
def contain_same_characters_and_order(s1, s2):
    s1_tmp = "".join(OrderedDict.fromkeys(s1))
    s2_tmp = "".join(OrderedDict.fromkeys(s2))
   
    for i in range(0, len(s1_tmp)):
        if (s1_tmp[i] != s2_tmp[i]):
            return False
       
    return True
   
 
s1 = "python"
s2 = "pppppytttthoooonn"
 
if (contain_same_characters_and_order(s1, s2)):
    print("yes")
else:
    print("no")
 
 
'''
run:
   
yes
   
'''

 



answered Oct 29, 2019 by avibootz

Related questions

...