← 返回 cisco 的题目列表Nested Pattern String Expansion
类型:qbank
Expand strings where parenthesized groups are followed by repeat counts in curly braces, including nested groups such as `(a(bc){2}){2}`.
Requirements
Input is one string inputStr.
A pattern is a substring enclosed in parentheses, followed by a repeat count enclosed in curly braces.
Expand each pattern by concatenating it with itself the requested number of times.
Pattern-count pairs can be nested, such as (ef((ab){2}cd){2}){2}.
Multi-digit counts can appear.
Some inputs may contain spaces; skip irrelevant spaces if the statement permits them.
Examples
Input:
(ab){3}
Output:
ababab
Input:
(a(bc){2}){2}
Output:
abcbcabcbc
Notes
A stack-based parser is the natural fit: keep the string accumulated before (, parse the substring up to ), read the following {number}, then repeat and append.
Java candidates hit errors when comparing char to String, for example using != "}" instead of comparing against '}'.
Hidden cases can stress nested groups and multi-digit repeat counts.