Amazon OA Interview Question 1
292

A product name at Amazon is represented as a string s of length n that consists of lowercase

English letters.

A team at Amazon working on a product's search algorithm.

They want to know the length of the longest substring in a product name with its first character exicographically smaller than its last. A valid substring must be longer than 1 character. If no such substring exists, return 0.

Note: A character a is lexicographically smaller than character b if a appears in the English alphabet sequence before b.

Example

The string s = "ecbdca".

s[2:4]

e

b

d

There are two longest valid substrings:

"cbd", s[1:3] shown, and "bdc", s(2.4].

Both start with a smaller character than they end with and have three characters."

s[1:3]

Return their length, 3. Note that "cdc" is not valid since the starting character is not smaller than the last.

Function Description

Complete the function findMaxChainLength in the editor below.

findMaxChainLength has the following parameter:

string s: the product name

Returns

int. the length of the longest valid substring

Constraints

2≤n$ 105
s consists of lowercase English letters only.

Comments (2)