--- id: 69cfca90e8a0a6d4d6871c4f title: "Challenge 265: Deepest Brackets" challengeType: 29 dashedName: challenge-265 --- # --description-- Given a string containing balanced brackets, return the content of the deepest nested brackets. - Brackets can be any of the three types: `()`, `[]`, and `{}`. - The input will always have a single deepest group. For example, given `"(hello (world))"`, return `"world"`. # --hints-- `get_deepest_brackets("(hello (world))")` should return `"world"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(get_deepest_brackets("(hello (world))"), "world")`) }}) ``` `get_deepest_brackets("[outer [inner] outer]")` should return `"inner"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(get_deepest_brackets("[outer [inner] outer]"), "inner")`) }}) ``` `get_deepest_brackets("{a{b}c{d{e}f}g}")` should return `"e"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(get_deepest_brackets("{a{b}c{d{e}f}g}"), "e")`) }}) ``` `get_deepest_brackets("[the {quick (brown [fox] jumped) over (the) lazy} dog]")` should return `"fox"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(get_deepest_brackets("[the {quick (brown [fox] jumped) over (the) lazy} dog]"), "fox")`) }}) ``` `get_deepest_brackets("f[(r)e{e}C{o[(d){e(C)}a]m}]p")` should return `"C"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(get_deepest_brackets("f[(r)e{e}C{o[(d){e(C)}a]m}]p"), "C")`) }}) ``` # --seed-- ## --seed-contents-- ```py def get_deepest_brackets(s): return s ``` # --solutions-- ```py def get_deepest_brackets(s): max_depth = 0 depth = 0 content = '' start = 0 for i, ch in enumerate(s): if ch in '([{': depth += 1 if depth > max_depth: max_depth = depth start = i + 1 elif ch in ')]}': if depth == max_depth: content = s[start:i] depth -= 1 return content ```