Playing with the strings.

Easy
0/40
Average time to solve is 15m
profile
Contributed by
5 upvotes
Asked in companies
SprinklrAmazon

Problem statement

Kevin loves playing with the strings only when they have unique characters. Once his father gave him a string that contains various characters. You have to find out whether Kevin will play with it or not.

Note:

The string may contain English alphabets, numbers, and special characters.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain a string ‘S’.
Output Format:
For each test case, return 'true' if Kevin will play with the given string. Otherwise, return 'false'.

Print the output for each test case in a separate line.
Note:
You don’t need to print anything, It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 1000
1 <= |S| <= 100

Where '|S|' is the length of the given string.

Time limit: 1 sec
Sample Input 1:
2
Coding$ninjas
coding123  
Sample Output 1:
NO
YES
Explanation of sample input 1:
In test case 1, the string has ‘n’ and ‘i’ as repeated characters.

In test case 2, the string has all characters unique.
Sample Input 2:
3
A
@#!$
abab
Sample Output 2:
YES
YES
NO
Explanation for Sample Output 2:
In test case 1, the string has only a single character.

In test case 2, the string does not have any repeated character.

In test case 3, the string has ‘a’ and ‘b’ as repeated characters.
Hint

Can you think of checking the uniqueness of all characters using a loop?

Approaches (3)
Brute Force

The basic idea is to iterate through all the array elements and check whether each character uniquely exists in the string or not. 

 

The steps are as follows:

 

  1. Iterate through the string ‘S’ (say, iterator = ‘i’).
    • Iterate through the string ‘S’ again but from (‘i’ + 1) to the string’s end.
      • Check If anywhere found the same characters then return false.
  2. Return true if there exist unique characters.
Time Complexity

O(N ^ 2), Where ‘N’ is the length of the given string ‘S’.

 

Since we are using a nested loop through the string S, so the overall time complexity will be O(N ^ 2).

Space Complexity

O(1)

 

Since we are not using any extra space, the overall space complexity will be O(1).

Code Solution
(100% EXP penalty)
Playing with the strings.
Full screen
Console