Pascal case of a given sentence

Easy
0/40
5 upvotes
Asked in companies
OracleAricent Technologies (Holdings) LimitedDaffodil Software

Problem statement

Given a string “STR”, you need to remove spaces from the string “STR” and rewrite in the Pascal case. Your task is to return the string “STR”.

In the Pascal case writing style we don’t have spaces between words and all words begin with uppercase letters including the first word.

Note:

Input string “STR” will only consist of lowercase English Alphabets. The string will not contain white space at the beginning and end of the string.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.

The first and the only line of each test case contains the string 'STR'.   
Output Format :
For each test case, return a string that is written in the Pascal case style.

Output for each test case will be printed in a new line. 
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= |STR| <= 10^5

Where |STR| denotes the length of “STR”

Time Limit: 1sec
Sample Input 1 :
2
coding ninja is great
i am pascal case
Sample Output 1:
CodingNinjaIsGreat
IAmPascalCase

Explanation For Sample Output 1:

In the first case, 
We have four words “coding”, “ninja, “is”, “great”. After we remove the space between these words and change the first letter of each word to an uppercase letter. the resulting string will be “CodingNinjaIsGreat” in the pascal case.


In the second case, 
We have four words “i”, “am”, “pascal”, “case”. After we remove the space between these words and change the first letter of each word to an uppercase letter. the resulting string will be “IAmPascalCase” in the pascal case.
Sample Input 2 :
2
aba cbs llb
a b c d 
Sample Output 2 :
AbaCbsLlb
ABCD
Hint

How can you find the first letter of each word?

Approaches (1)
Brute Force

We can iterate over the string “STR” and maintain the resultant string in “answer”. On each character check if that character is white space (‘ ’) then change the next character to uppercase and do not include white space in the string “answer”, else append that character in the string “answer”.

 

Algorithm:

 

  • Create an empty string “answer”.
  • Iterate over string “STR”, for each character
    • If the STR[i] == ‘ ‘:
      • Change STR[i + 1] to upper case.
    • Else
      • Append this character in “answer”.
  • Return “answer”.
Time Complexity

O(N), where ‘N’ denotes the length of STR.

 

Since we iterate through the string “STR” only once, the time complexity is O(N).

Space Complexity

O(N), where ‘N’ denotes the length of STR.

 

Since we store at most ‘N’ characters in the string “answer”, the space complexity is O(N).

Code Solution
(100% EXP penalty)
Pascal case of a given sentence
Full screen
Console