티스토리 뷰

Question

Write a function that reverses characters in (possibly nested) parentheses in the input string.

Input strings will always be well-formed with matching ()s.


Example

  • For inputString = "(bar)", the output should be reverseInParentheses(inputString) = "rab";
  • For inputString = "foo(bar)baz", the output should be reverseInParentheses(inputString) = "foorabbaz";
  • For inputString = "foo(bar)baz(blim)", the output should be reverseInParentheses(inputString) = "foorabbazmilb";
  • For inputString = "foo(bar(baz))blim", the output should be reverseInParentheses(inputString) = "foobazrabblim". Because "foo(bar(baz))blim" becomes "foo(barzab)blim" and then "foobazrabblim".

Input/Output

  • [execution time limit] 4 seconds (py3)

  • [input] string inputString

    A string consisting of lowercase English letters and the characters ( and ). It is guaranteed that all parentheses in inputString form a regular bracket sequence.

    Guaranteed constraints: 0 ≤ inputString.length ≤ 50.

  • [output] string

    • Return inputString, with all the characters that were in parentheses reversed.

Best_ANSWER

def reverseInParentheses(inputString):
    for i in range(len(inputString)):
        if inputString[i] == "(":
            start = i
        if inputString[i] == ")":
            end = i
            return reverseInParentheses(inputString[:start] + inputString[start+1:end][::-1] + inputString[end+1:])
    return inputString
  • 어렵다.... 답을 봄

'Codesignal' 카테고리의 다른 글

code signal - 15. Add Border  (0) 2020.02.16
code signal - 14. alternatingSums  (0) 2020.02.10
code signal - 12. Sort by Height  (0) 2020.01.29
code signal - 11. isLucky  (0) 2020.01.29
code signal - 10. commonCharacterCount  (0) 2020.01.29
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함