BackmediumStrings Zomato PhonePe

Recipe Validator Solution

Problem Statement

Given a string representing the order of ingredients, return the length of the longest subsequence where ingredients alternate between 'S' and 'T'.

Example 1
Input
SST
Output
3

Explanation: Step-by-step: with input SST, we start with 'S', then 'T', then 'S', giving output 3. The longest subsequence where ingredients alternate between 'S' and 'T' is 'S-T-S'.

Example 2
Input
TSTT
Output
2

Explanation: Step-by-step: with input TSTT, we start with 'T', then 'S', then 'T', but the last character 'T' does not match 'S', giving output 2. The longest subsequence where ingredients alternate between 'S' and 'T' is 'T-S-T'.

Constraints

  • 1 <= input length <= 500
  • The input string contains only 'S' and 'T' 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