Consecutive Author Constraint — Problem Statement & Solution Guide
Problem Description
Given an array of author identifiers authors where each element represents the author of a chapter, determine the maximum length of a subsequence where no two adjacent elements have the same author identifier and the first and last elements are distinct.
Examples
Input
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
Output
25
Explanation: Step-by-step: with input ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'], we can form a subsequence of length 25 by selecting every other element starting from the first element.
Input
['A', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
Output
24
Explanation: Step-by-step: with input ['A', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'], we can form a subsequence of length 24 by selecting every other element starting from the first element, skipping the first 'A'.
Constraints
- The input string length will be between 2 and 500 characters
- Each character represents a unique author
Optimal Approach & Strategy
The optimal approach uses dynamic programming to build up a solution by maintaining two arrays that track the longest subsequences with a different author for the last two chapters. This approach reduces the time complexity to linear.
Brute Force Approach
The brute-force approach involves checking every possible subsequence of the input string, resulting in a time complexity of O(2^n). This is inefficient and only suitable for very small inputs. The naive approach checks all subsequences, leading to an exponential time complexity.
Verified Code Solutions
function maxConsecutiveAuthors(authors) {
if (authors.length === 0) return 0;
let maxLength = 1;
let currentLength = 1;
for (let i = 1; i < authors.length; i++) {
if (authors[i] !== authors[i - 1]) {
currentLength++;
} else {
maxLength = Math.max(maxLength, currentLength);
currentLength = 1;
}
}
return Math.max(maxLength, currentLength);
}function maxConsecutiveAuthors(authors) {
if (authors.length === 0) return 0;
let maxLength = 1;
let currentLength = 1;
for (let i = 1; i < authors.length; i++) {
if (authors[i] !== authors[i - 1]) {
currentLength++;
} else {
maxLength = Math.max(maxLength, currentLength);
currentLength = 1;
}
}
return Math.max(maxLength, currentLength);
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.