Crash Code Part-3 (Word shortening with duplicates)
Trying to solve old coding questions, because reasons
519 Words | Reading Time: 2 Minutes, 21 Seconds
02-01-2023 12:00 AM UTC
Following question was asked in
IBM Corporation-Campus Hiring- 18-Jan-23_B2
. Test duration: 30 mins
Problem Description
A particular application uses the following logic to reduce length of sentences by shortening the individual words:
- Take each word and remove the vowels in it to get its shortened form
- It is possible that two different words have the same shortened form. In this case:
- The first shortened word is used as-is
- For the second shortened word that is same as the first, number 1 is appended at the end
- For the third shortened word that is same as the first, number 2 is appended at the end and so on.
Given a sentence, the program should print the final shortened version of the sentence based on the rules above.
For example, short form for both ‘build and ‘bold’ would be ‘bid’ as per the rules above. The numbers to be added will depend on the order of the words in the sentence
Function Description:
Complete the function shorten5entence in the editor below. The function must print the final shortened version of the sentence based on the rules above
shorten5entence has the following parameter(s): sentence: a String
Constraints
- Only lowercase letters would be present in the sentence
Examples
Sample Case 0
Sample Input For Custom Testing
it was bold to make the build larger
Sample Output
t ws bld t1 mk th bld1 lrgr
Explanation:
The words are shortened by removing the vowels from each word. When there are repeating words in shortened form, the first one is used as-is and the second one onwards a number is appended. Thus, ‘it’ becomes ’t’,’to’ becomes t1’. Similarly ‘bold’ becomes ‘bld’ and ‘build’ becomes ‘bld1’.
Sample Case 1
Sample Input For Custom Testing
we made the builds larger
Sample Output
w md th blds lrgr
Explanation:
The words are shortened by removing the vowels from each word. Here all shortened versions are distinct, hence no need to append numbers.
Solve
|
|