BackmediumTwo Pointers Amazon TCS

Warehouse Packaging Solution

Problem Statement

Given a sequence of package sizes represented as a string of 'L' (large) and 'S' (small), find the length of the longest contiguous subsequence that has an equal number of 'L' packages and 'S' packages and starts and ends with the same package size.

Example 1
Input
LL
Output
0

Explanation: Step-by-step: Given the input 'LL', we initialize two pointers, left and right, to 0. We then enter a loop where we increment the count of 'L' packages and 'S' packages. However, since the counts are never equal and the subsequence does not start and end with the same package size, we return 0 as the longest contiguous subsequence length.

Example 2
Input
SSLS
Output
2

Explanation: Step-by-step: Given the input 'SSLS', we initialize two pointers, left and right, to 0. We then enter a loop where we increment the count of 'L' packages and 'S' packages. When the counts are equal and the subsequence starts and ends with the same package size, we update maxLen. In this case, maxLen is updated to 2 when the subsequence 'SS' meets the conditions.

Constraints

  • 1 <= length of the input string <= 1000
  • The input string only contains 'L' and 'S' characters.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free