[Solved] Remove all the repeated characters and print the string with the distinct characters

Distinct Characters:

Take a string from the user.

Remove all the repeated characters and print the string with the distinct characters.

Constraints

1 <= length of the string <= 50

Sample Input 0

aabbbbcccdddeeeefff

Sample Output 0

abcdef

Solution

Approach:

The input is taken in the variable s.

The variable p is initialized with an empty string.

The for loop iterates through the string s.

If the character is not present in the string p, then the character is added to the string p.

If the length of the string p is equal to the length of the string s, then the output is invalid.

If the length of the string p is not equal to the length of the string s, then the string p is printed.

s = input()
p = ""
for i in s:
    if i not in p:
        p += i
if len(p) == len(s):
    print("invalid")
else:
    print(p)
    

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *