Incremental String Qualification with Split and Merge
Learn this problemProblem statement
An ordered list of nonempty strings, initialParts, represents one logical string partitioned into adjacent parts. You will process a finite sequence of split and merge operations while preserving the order of all characters.
Operations
["SPLIT", i, offset]: replace partiwith its nonempty prefix of lengthoffsetfollowed by its remaining nonempty suffix.["MERGE", i]: replace partsiandi + 1with their concatenation.
After each operation, classify the current partition:
QUALIFIEDif every current part has length at mostmaxPartLength.UNQUALIFIEDotherwise.
Return one classification per operation, in the same order as operations.
Function
classifyParts(initialParts: String[], operations: String[][], maxPartLength: int) → String[]Examples
Example 1
initialParts = ["abcdef"]operations = [["SPLIT","0","3"],["MERGE","0"],["SPLIT","0","2"],["SPLIT","1","2"]]maxPartLength = 3return = ["QUALIFIED","UNQUALIFIED","UNQUALIFIED","QUALIFIED"]Splitting abcdef at offset 3 produces [abc, def], so both parts qualify. Merging restores one length-6 part. The next split produces lengths 2 and 4, and the final split produces three length-2 parts.
Example 2
initialParts = ["ab","cdef"]operations = [["MERGE","0"],["SPLIT","0","4"]]maxPartLength = 4return = ["UNQUALIFIED","QUALIFIED"]The merge creates abcdef, whose length exceeds 4. Splitting it into abcd and ef makes every part short enough.
Example 3
initialParts = ["a","bc","def"]operations = [["MERGE","0"],["MERGE","0"],["SPLIT","0","3"]]maxPartLength = 3return = ["QUALIFIED","UNQUALIFIED","QUALIFIED"]The first merge creates abc, which still qualifies. The second creates one length-6 part, and splitting it at offset 3 restores two qualified parts.
Constraints
1 ≤ initialParts.length ≤ 2000.1 ≤ operations.length ≤ 2000.1 ≤ maxPartLength ≤ 100.- Each initial part contains only lowercase English letters and has length in
[1, 100]. - The total number of characters across all parts is at most
2000and remains constant. - Every operation is either
SPLITorMERGEwith the exact arity shown above. - Every split index and offset are valid for the current partition and produce two nonempty parts.
- Every merge index has a right-adjacent part in the current partition.