mediumStringsPattern: Mixed
Alternate Ingredient Sequence Length Solution
Problem Statement
Given a string of characters representing ingredient categories, where 'A' denotes one category and 'B' denotes another, determine the length of the longest subsequences that alternate between 'A' and 'B'.
Examples
Example 1:
Input:STST
Output:4
Explanation: The input 'STST' represents a sequence of ingredients where 'S' and 'T' alternate, resulting in a longest subsequence of length 4.
Example 2:
Input:STT
Output:2
Explanation: The input 'STT' represents a sequence of ingredients where 'S' and 'T' do not perfectly alternate, resulting in a longest subsequence of length 2.
Constraints
- 1 <= input length <= 500
- The input string contains only 'S' and 'T' characters.
Time: O(n) Space: O(n)
The optimal approach uses dynamic programming to keep track of the longest alternating subsequences ending at each position, with a time complexity of O(n) and a space complexity of O(n).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
