Deja Vu

wordcloud

Note: This page contains a small but interesting piece of Python code which I call snippets. You can find more such codes on my Python snippets page.

Problem

You aren't paying attention and you accidentally type a bunch of random letters on your keyboard. You want to know if you ever typed the same letter twice, or if they are all unique letters.

Task:
If you are given a string of random letters, your task is to evaluate whether any letter is repeated in the string or if you only hit unique keys while you typing.

Input Format:
A string of random letter characters (no numbers or other buttons were pressed).

Output Format:
A string that says 'Deja Vu' if any letter is repeated in the input string, or a string that says 'Unique' if there are no repeats.

Sample Input:
aaaaaaaghhhhjkll

Sample Output:
Deja Vu

Solution

Here is my solution to the above problem. Remember that there could be more than one way to solve a problem. If you have a more efficient or concise solution, please leave a comment.

In [1]:
x=input()
if len(set(x))<len(x):
    print("Deja Vu")
else:
    print("Unique")
Deja Vu

Explanation

My approach (or the algorithm)

The goal is to find out if there is any repetition of letters or not. We can extract all unique letters from a string and count them. If we compare this count with the count of letters in the string, we can tell whether there was any repetition or not. If there is no repetition the both counts will be the same. If there is any repitition then letter count in the string will me greater than the unique letters count.

The code

  • Extract unique letters set(x)
  • Compare letter counts using len()
  • If equal then print("Unique") else print("Deja Vu")

The problem question is picked from SoloLearn. Here is my SoloLearn code and my SoloLearn profile page.

Last updated 2021-01-09 14:23:09.780057 IST

Comments