Facebook Pixel

Amazon Online Assessment 2021 (OA) - Common Prefix

This text talks about Kindle Direct Publishing; it's a platform for self-publishing e-books and it's busy making a new feature that assists authors in keeping tabs on how text characters are used in numerous ways. Currently, they're in the process of testing out a part of the function that handles text prefixes and suffixes, with a little assistance from others.

Here's what's going on: they have a string and they want to break it up into two substrings - divide it at all possible points. Now, the substring at the end, that's called a suffix. Conversely, the substring at the start is referred to as a prefix. So, what they want to find out is the lengths of the same prefix between each suffix and the original string. Sounds complicated, right? But all it really means is finding how much of the start of the suffix matches with the start of the original string. Once they have this information, they want to add up these lengths and return the total.

From what we gather, they're going to return this information as an array, where each item i represents the total for string i.

Relevant Amazon OA Problems:

Input

  • inputs: an array of strings

Output

the sums of the common prefix lengths for each test case

Examples

Example 1:

Input:

s = ['abcabcd']

Output: [10]

Explanation:

Remove to leave suffixSuffixCommon PrefixLength
'''abcabcd''abcabcd'7
'a''bcabcd'''0
'ab''cabcd'''0
'abc''abcd''abc'3
'abca''bcd'''0
'abcab''cd'''0
'abcabc''d'''0

The sum is 7 + 0 + 0 + 3 + 0 + 0 + 0 = 10.

Constraints

  • 1 <= n <= 10
  • 1 <= |inputs[i]| <= 10^5
  • Each inputs[i] contains only lowercase English letters

Try it yourself

Solution

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro