Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.1 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69cfca90e8a0a6d4d6871c4f Challenge 265: Deepest Brackets 29 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".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_deepest_brackets("(hello (world))"), "world")`)
}})

get_deepest_brackets("[outer [inner] outer]") should return "inner".

({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".

({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".

({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".

({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--

def get_deepest_brackets(s):

    return s

--solutions--

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