Prepin
Log in

A

Shorten a collection of strings.

Approach / Explanation

This question requires you to create an algorithm that can shorten an array of strings. "Abbreviation" involves condensing each string in the array in some manner, which may vary based on specific requirements. Generally, this means substituting a part of the string with a shorter version. Common methods for abbreviating a string include: replacing a consecutive sequence of characters with the length of that sequence; eliminating vowels; transforming a word into an acronym. Before proceeding to answer the question, it's important to clarify the requirements. Discussing and understanding the expected results is essential. Once that is established, you can begin to outline how you would create an algorithm to achieve this task. When designing your algorithm, consider whether each string should be processed uniformly, if there are any exceptions, or if there are specific edge cases that require careful handling.

Suggested Answer

When aiming to shorten strings in an array by substituting each word with its first and last character, along with the count of characters in between, here’s a Python solution: def abbreviate_strings ( word_list ) : new_list = [ ] for i in word_list : if len ( i ) > 2 : new_word = i [ 0 ] + str ( len ( i ) - 2 ) + i [ - 1 ] new_list . append ( new_word ) else : new_list . append ( i ) return new_list This function iterates through each string in the given list. For strings with more than 2 characters, it constructs a new string that includes the first character, the count of characters between the first and last character, followed by the last character. This newly formed string is then added to the list. If the string has 2 or fewer characters, it is simply appended to the list unchanged.

Alternative Answer

If the goal is to substitute the vowels in the strings with blanks, here is a Python solution: def abbreviate_strings ( word_list ) : vowels = 'aeiou' new_list = [ '' . join ( char for char in word if char . lower ( ) not in vowels ) for word in word_list ] return new_list This solution makes use of list comprehension and the string method 'join'. For each word in the input list, it inspects each character. If a character is not a vowel (a, e, i, o, u), it will be included in the result. All such characters are then combined back into a string using the 'join' method. The final outcome is an array that holds the modified strings.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Apple

Product

AI Candidate AgentCompaniesBrowse JobsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin | All rights reserved.