Huawei OD computer test - simplifying complex strings (Java & JS & Python)

Question description

Given an input string, the string may only consist of English letters ('a' ~ 'z', 'A' ~ 'Z' ) and left and right parentheses ( '(', ')' ).

When there are parentheses in a character, the parentheses are in pairs. There can be one or more parentheses pairs. The parentheses pairs will not be nested. The parentheses pairs can contain one or more English letters, or not. Contains English letters.

When a pair of parentheses contains multiple English letters, these letters are equivalent to each other, and the equivalence relationship can be transferred between different pairs of parentheses, that is, when 'a' and 'b' are equivalent When 'b' and 'c' are equivalent, 'a' and 'c' are also equivalent. In addition, uppercase letters and lowercase letters of the same English letter are also equivalent to each other (even if they are distributed in different bracket pairs) )

This input string needs to be simplified and a new string output. In the output string, only the characters in the input string that are not included in the parentheses are retained (in the order of the characters in the input string), and each character is added to the input string. characters are replaced by the lexicographically smallest equivalent character contained within the pair of parentheses.

If the simplified string is empty, output "0".

Example:
The input string is "never(dont)give(run)up(f)()", and the initial equivalent character set is ('d', 'o', 'n', 't'), ('r ', 'u', 'n'), since the equivalence relationship can be transferred, the final equivalent character set is ('d', 'o', 'n', 't

おすすめ

転載: blog.csdn.net/qfc_128220/article/details/130765683