Get Longest Substring
Learn this problemProblem statement
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 lexicographically 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.
Function
getLongestSubstring(s: String) → int
Complete the function getLongestSubstring in the editor below.
getLongestSubstring has the following parameter:
string s: the product name
Returns
int: the length of the longest valid substring
𓇼 ⋆。˚Credit to Yogi 𓆝⋆。˚ 𓆡 𓈒🫧
Examples
Example 1
s = "ecbdca"return = 3
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.
Return their length, 3. Note that "cbdc" is not valid since the starting character is not smaller than the last.
Example 2
s = "abcd"return = 4
The entire string is a valid substring. Here, a is lexicographically
smaller than d, so it is a valid substring.
Example 3
s = "fghbbadcba"return = 5
The longest valid substring is "bbadc". Here, b is lexicographically
smaller than c, so it is a valid substring.
Constraints
2 ≤ n ≤ 10^5sconsists of lowercase English letters only.
More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026