Find common characters between two strings

Question:

I am trying to print the common letters from two different user inputs using a for loop. (I need to do it using a for loop.) I am running into two problems: 1. My statement “If char not in output…” is not pulling unique values. 2. The output is giving me a list of individual letters rather than a single string. I tried the split the output but split ran into a type error.

wrd = 'one'
sec_wrd = 'toe'

def unique_letters(x): 
    output =[]
    for char in x: 
        if char not in output and char != " ": 
            output.append(char)
    return output

final_output = (unique_letters(wrd) + unique_letters(sec_wrd))

print(sorted(final_output))
Asked By: JMatth

||

Answers:

You are trying to perform the Set Intersection. Python has set.intersection method for the same. You can use it for your use-case as:

>>> word_1 = 'one'
>>> word_2 = 'toe'

#    v join the intersection of `set`s to get back the string
#    v                             v  No need to type-cast it to `set`.
#    v                             v  Python takes care of it
>>> ''.join(set(word_1).intersection(word_2))
'oe'

set will return the unique characters in your string. set.intersection method will return the characters which are common in both the sets.


If for loop is must for you, then you may use a list comprehension as:

>>> unique_1 = [w for w in set(word_1) if w in word_2]
# OR
# >>> unique_2 = [w for w in set(word_2) if w in word_1]

>>> ''.join(unique_1)  # Or, ''.join(unique_2)
'oe'

Above result could also be achieved with explicit for loop as:

my_str = ''
for w in set(word_1):
    if w in word_2:
        my_str += w

# where `my_str` will hold `'oe'`
Answered By: Moinuddin Quadri

For this kind of problem, you’re probably better off using sets:

wrd = 'one'
sec_wrd = 'toe'
wrd = set(wrd)
sec_wrd = set(sec_wrd)

print(''.join(sorted(wrd.intersection(sec_wrd))))
Answered By: match

Function to solve the problem

def find_common_characters(msg1,msg2):
    #to remove duplication set() is used.
    set1=set(msg1)
    set2=set(msg2)
    remove={" "}
    #if you wish to exclude space
    set3=(set1&set2)-remove
    msg=''.join(set3)
    return msg

Providing input and Calling the function
Provide different values for msg1,msg2 and test your program

msg1="python"
msg2="Python"
common_characters=find_common_characters(msg1,msg2)
print(common_characters)
Answered By: Siddharth J

I have just solved this today on code signal. It worked for all tests.

def solution(s1, s2):

    common_char = ""
    for i in s1:
        if i not in common_char:
        
            i_in_s1 = s1.count(i)
            i_in_s2 = s2.count(i)
        
            comm_num = []
            comm_num.append(i_in_s1)
            comm_num.append(i_in_s2)
        
            comm_i = min(comm_num)
            new_char = i * comm_i
            common_char += new_char
    
  
    return len(common_char)
Answered By: Cat

Here is your one line code if you want the number of common character between them!

def solution(s1,s2):
 return sum(min(s1.count(x),s2.count(x)) for x in set(s1))
Answered By: mo1010
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.