← 返回 ibm 的题目列表Minimum Insertions to Form Repeated abc Pattern
类型:qbank
Given a lowercase string, insert the fewest characters needed so the result is a repetition of `abc` blocks. Deletion and replacement are not allowed.
Requirements
Input: a lowercase string s.
Operation: insert any lowercase character at any position.
Output: the minimum number of insertions needed to transform s into a string of the form "abcabcabc...".
Original characters must remain in order; deletion and replacement are not allowed.
Examples
Input: "a"
Output: 2
Insert b and c to form abc.
Input: "aa"
Output: 4
Insert b, c, b, and c to form abcabc.
Notes
Simulate matching against the infinite pattern abcabc....
Maintain one pointer into s and one pointer into the target pattern. When s[i] matches the expected pattern character, consume it; otherwise count an insertion.
After all original characters are consumed, add the insertions needed to finish the current abc block.
Preparation
Solve the canonical abc valid-string problem three ways: direct expected-character simulation, block counting, and a small-state greedy scan.
Rehearse tail handling separately; after consuming the input, the current partial block must still be completed.