Check if the letters of one string are in another string

Viewed 5866

Okey so basically I have to check if one string has the same letters of another string. Both strings are obtained via input().

I don't want to double check if a letter is in the other string, so if I already checked that letter I want to skip to the next letter.

The code I have for now is this:

str1, str2 = list(input()), list(input()) 

if len(str1) > len(str2):
    str = str1
else: 
    str = str2

for x in str:
    c = 0
    if x in str2:
        c += 1
if c != 0:
    print("Both have the same letters!") 
else: 
    print("Nope there are some letters missing..")

I don't know if I should work with the lists instead of using a counter.. please a detailled explanation of the solution or some good quality guidance would be very appreciated! <3

4 Answers

Converting strings to sets of individual symbols remove duplicate symbols, so we can simply compare them:

if set(str1) == set(str2):
    print("Both have the same letters!") 
else:
    print("Nope there are some letters missing..")

Note:

As the order of elements in sets is not important, we may even compare them, e. g.

if set(str1) <= set(str2):        # <= means "is subset" in this context
    print("All symbols in str1 are in str2, too.")

or

if set(str1) < set(str2):        # < means "is a proper subset" in this context
    print("All symbols in str1 are in str2, too, "
          "but str2 has at least 1 symbol not contained in str1.")

Some issues with the code are that, str2 is always used for comparison even if str2 is longer than str1

for x in str:
    c = 0
    if x in str2:

Next, c is set to 0 for every character in str, Instead you can have a counter to count the number of chars not in the other string

This should do the job

str1, str2 = list(input()), list(input()) 

if len(str1) > len(str2):
    str = str1
    compared_string = str2
else: 
    str = str2
    compared_string = str1

not_in_other_string = 0
for x in str:
    if x  not in compared_string:
        not_in_other_string += 1
if not_in_other_string == 0:
    print("Both have the same letters!") 
else: 
    print("Nope there are some letters missing..")

It sounds like you want to find if all of the characters in one string are in some larger string.

You could use unique = ''.join(set(substring)) to get a list of chars in the substring and then make a list comprehension to get all the chars in the larger string.

Here is my example:

unique = ''.join(set(substring))
not_in = [char for char in unique if char not in superstring]

Now you can check not_in to see if it is null, otherwise there was a char in the substring that was not in the superstring.

For example:

superstring = "hello"
substring = "xllo"

unique = ''.join(set(substring))
not_in = [char for char in unique if char not in superstring]

print not_in
>>>['x']

print not_in == []
>>>False

Typically, the preferred way to do things in python is using list comprehensions or use some built in function that already checks strings for you, rather than the C style where you go through a loop and check letter for letter explicitly. This is the idiomatic way of pythonic programming.

Breaking apart the list comprehension we have a loop like this:

   for char in unique: 
        if char not in superstring:
             char #char meets conditions so it is the value used in [char ... 

Here would be the modification I would make

str1, str2 = input(), input()

unique = ''.join(set(str1))
not_in = [char for char in unique if char not in str2]

if not_in == []:
    print("Both have the same letters!") 
else: 
    print("Nope there are some letters missing..")
str1, str2 = input().split()

if len(str1) > len(str2):
    string_to_check= str1
    string_from_which_to_check = str2
else: 
    string_to_check = str2

string_from_which_to_check = str1    
not_in_other_string  = set(string_to_check) - set(string_from_which_to_check)
if len(not_in_other_string )==0:
     print("character are present in string")
else:
    print("{} not present in string".format(str(not_in_other_string )))
Related