Description
Solutions
Optimize Identifiers
๐Ÿ‘ฉโ€๐ŸŽ“ NEW GRAD

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

The analyst wants to optimize the product identifiers by performing a series of operations on the string to maximize the number of operations 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 optimizeIdentifiers in the editor.

optimizeIdentifiers has the following parameter:

  • 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.

๐“‡ผ โ‹†.หšManyy Manyyy thanks to Nachos and spike!๐“† ๐“†กโ‹†.หš ๐“‡ผ

Example 1:

Input:  s = "babdcaac"
Output: 5
Explanation:
The type of the initial string is "bc". 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.

Example 2:

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

Example 3:

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.
Constraints:
  • 2 โ‰ค |s| โ‰ค 2 * 105
  • String s consists of lowercase English letters only.
Thumbnail 0
Thumbnail 1
Testcase

Result
Case 1

input:

output: