FastPrepFastPrep
Problem Brief

Optimized Identifiers

OA
See Amazon online assessment and hiring insights

In an Amazon inventory management, an operations analyst is dealing with a set of initial product identifiers represented by strings. The type of a string product identifier is determined by the first and last letters in the identifier string, for example, the type of the identifier string "abddaac" is "ac".

The analyst wants to optimize the product identifiers by performing a series of operations on the string to maximize the number of operation between the final and initial types. Given a product identifier string s, the analyst can perform one operation at a time, involving the removal of either the first or last letter from the string.

Find the maximum number of operations they can perform on the string while ensuring that its type aligns with the initial string's type.

Note: The type of an empty string" is", and the type of a string with a single character, like "a", is "aa".

Function Description

Complete the function optimizedIdentifiers in the editor below.

optimizedIdentifiers has the following parameter:

  • 1. string s: the initial product identifiers.

Returns

int: the maximum number of operations the operations analyst can perform on the string, such that its type is equal to the initial string's type.

1Example 1

Input
s = "hchc"
Output
2
Explanation
The type of the initial string is "h", so the operations analyst can remove the first 2 or last 2 letters and get the string "h" with a type "h", hence the answer is 2.

2Example 2

Input
s = "abbc"
Output
0
Explanation
The operations analyst can't remove any letters from the string since the type will change, hence the answer is 0.

3Example 3

Input
s = "babdcaac"
Output
0
Explanation
The type of the initial string s is "bc": Final String, Number of Operations, Type babdcaac, 0, bc bdcaac, 6, bc d, 7, dd bdc, 5, bc babdc, 3, bc From the above final strings, the only valid final strings (strings with a type equal to the type of the initial string "bc") are "babdcaac", "bdc", and "babdc" with a total of operations performed of 0, 5, and 3 respectively, hence the answer is 5.

Constraints

Limits and guarantees your solution can rely on.

  • 2 ≤ |s| ≤ 2 * 10^5
  • String s consists of lowercase English letters only.
  • public int optimizedIdentifiers(String s) {
      // write your code here
    }
    
    Input

    s

    "hchc"

    Output

    2

    Sign in to submit your solution.