Ninja And Editor

Easy
0/40
Average time to solve is 20m
profile
Contributed by
21 upvotes
Asked in company
Adobe

Problem statement

Ninja wants to print a book of stories. He created a doc file and sent it to his editor to make some edits. But the file got corrupted due to some reasons and made changes in the original file. Ninja did not have a duplicate file of the same, so he wants to correct the same file. He found that the file has been changed in such a way that all the spaces have been removed from the file and the first letter after each space that used to be has been changed to the equivalent uppercase characters.

Example:
If the corrupted file looks like “CodingNinjasIsACodingPlatform”, then the original file was: “coding ninjas is a coding platform”.

Ninja needs to change the corrupted file to the original file.

Note:
You need to convert all the uppercase characters to lowercase characters, and you need to add a single space between every two words.
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’, which denotes the corrupted string that you need to to change.
Output Format:
For each test case, print a single line containing a string denoting the original sentence.

The output for every test case will be printed 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 <= 50
0 <= size of S <= 10000

where ‘T’ is the number of test cases.
where 'S’ is the corrupted string that you need to to change.

Time limit: 1 sec
Sample Input 1:
2
CodingNinjasIsACodingPlatform
Hello
Sample Output 1:
coding ninjas is a coding platform
hello
Explanation of sample input 1:
In the first test case, 
After replacing the uppercase characters with a space followed by the lowercase conversion of the character we get: 
coding ninjas is a coding platform

In the second test case, 
After replacing the uppercase characters with a space followed by the lowercase conversion of the character we get: 
hello
Sample Input 2:
3
HelloWorld
ILoveCoding
YouCanPracticeCodingOnCodezen
Sample Output 2:
hello world
i love coding
you can practice coding on codezen
Hint

Can you think of checking each character?

Approaches (1)
Naive

    The basic approach is to traverse through the complete string of characters and check if the character is in uppercase or lowercase. If it is in uppercase, simply update the character with a space followed by the lowercase conversion of the character.

 

Algorithm: 

  • Traverse through the string of characters and check for characters:
    • If lowercase:
      • Continue
    • Else -> uppercase:
      • Change it with a space followed by lowercase conversion.
  • Return the updated string
  • The corner case in every string is that if the first character is in uppercase, then we need not print a space before it. So, we will check for the first character of the string separately:
    • If lowercase:
      • Continue
    • Else -> uppercase:
      • Change it with lowercase characters.
Time Complexity

O(N), where ‘N’ is the size of the string.

 

We need to traverse through the complete string only once. So, the time complexity is O(N).

Space Complexity

O(1).

 

No extra space is required. So, space complexity is O(1).

Code Solution
(100% EXP penalty)
Ninja And Editor
Full screen
Console