HomeLanguagesPython program to find number of likes and dislikes

Python program to find number of likes and dislikes

Alice and Bob made a list of topics and both of them voted if they like or dislike the topic. They wrote 0 to denote dislike and 1 to denote like. The task is to count the number of topics that both of them like or dislike. Examples:

Input: alice = “010101” bob = “101101” Output: 3 alice and bob both like 4th and 6th topics and dislike the 5th topic. Hence, the output is 3. Input: alice = “111111” bob = “000000” Output: 0 There are no common likes or dislikes between alice and bob. Hence, the output is 0.

Input: alice = “110000” bob = “110011” Output: 4 alice and bob both like 1st and 2nd topics and dislike 3rd and 4th topics. Hence, the result is 4.

Approach:

  • First, we have to iterate through each character of the string consisting of the likes and dislikes of alice.
  • Then, we have to compare it with the corresponding entry of the string consisting of the likes and dislikes of bob.
  • Finally, we will count the number of similar entries and print.

Below is the implementation. 

Python3




# function to obtain no
# of topics both alice and
# bob like
def commontopics(alice, bob):
  
 # initiate count with 0
 count = 0
  
 # iterating through alice
 for i in range(0,len(alice)):
   
  # comparing with corresponding
  # bob entry
  if alice[i] == bob[i]:
    
   # counting similar entries
   count += 1
    
 # printing the count  
 print(count)
 
#main function
def main():
  
 commontopics("010101", "101101")
 commontopics("111111", "000000")
 commontopics("110000", "110011")
 
  
# driver code
if __name__ == "__main__":
 main()
 
# This code is contributed by SrujayReddy


Output:

3
0
4

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Space Complexity: O(n)

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32548 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6922 POSTS0 COMMENTS
Nicole Veronica
12037 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12143 POSTS0 COMMENTS
Shaida Kate Naidoo
7057 POSTS0 COMMENTS
Ted Musemwa
7297 POSTS0 COMMENTS
Thapelo Manthata
7015 POSTS0 COMMENTS
Umr Jansen
7001 POSTS0 COMMENTS