BackmediumStrings TCS

Optimizing Textbook Sequences Solution

Problem Statement

Given a sequence of textbook chapters, find the length of the longest subsequence where no two consecutive chapters have the same author, and the authors of the first and last chapters are different.

Example 1
Input
['A', 'B', 'C', 'B', 'A']
Output
3

Explanation: Step-by-step: Given the input ['A', 'B', 'C', 'B', 'A'], we start from the beginning. The first chapter is 'A'. We can choose the next chapter as 'B' because its author is different from 'A'. Then, we can choose 'C' as the next chapter because its author is different from 'B'. However, we cannot choose 'B' as the next chapter because its author is the same as 'C'. Therefore, the longest subsequence where no two consecutive chapters have the same author is ['B', 'C', 'A'] with a length of 3.

Example 2
Input
["A","A"]
Output
0

Explanation: Step-by-step: Given the input ['A', 'A'], we start from the beginning. The first chapter is 'A'. However, we cannot choose the next chapter as 'A' because its author is the same as the first chapter. Therefore, the longest subsequence where no two consecutive chapters have the same author and the authors of the first and last chapters are different is an empty subsequence with a length of 0.

Constraints

  • The input string length will be between 2 and 500 characters
  • Each character represents a unique author
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