Exercise 68
Write a python algorithm that find the number of common characters in two strings s1 and s2. Example if s = "Hello" and s2 = "World", the shared characters are 'l' and 'o', then the algorithm returns 2.
Solution
s1 = "Hello"
s2 = "World"
# initializing counter
counter = 0
# creating and initializing a string by deleting the repeated characters in s1
s = ""
for x in s1:
if x not in s:
s = s + x
for x in s:
if x in s2:
counter = counter + 1
# display the number of common characters in strings s1 and s2
print(counter) # display 2
Younes Derfoufi
my-courses.net
my-courses.net