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

1.7 KiB

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

getDeepestBrackets("(hello (world))") should return "world".

assert.equal(getDeepestBrackets("(hello (world))"), "world");

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

assert.equal(getDeepestBrackets("[outer [inner] outer]"), "inner");

getDeepestBrackets("{a{b}c{d{e}f}g}") should return "e".

assert.equal(getDeepestBrackets("{a{b}c{d{e}f}g}"), "e");

getDeepestBrackets("[the {quick (brown [fox] jumped) over (the) lazy} dog]") should return "fox".

assert.equal(getDeepestBrackets("[the {quick (brown [fox] jumped) over (the) lazy} dog]"), "fox");

getDeepestBrackets("f[(r)e{e}C{o[(d){e(C)}a]m}]p") should return "C".

assert.equal(getDeepestBrackets("f[(r)e{e}C{o[(d){e(C)}a]m}]p"), "C");

--seed--

--seed-contents--

function getDeepestBrackets(str) {

  return str;
}

--solutions--

function getDeepestBrackets(str) {
  let maxDepth = 0, depth = 0, content = '';
  let start = 0;

  for (let i = 0; i < str.length; i++) {
    if ('([{'.includes(str[i])) {
      depth++;
      if (depth > maxDepth) {
        maxDepth = depth;
        start = i + 1;
      }
    } else if (')]}'.includes(str[i])) {
      if (depth === maxDepth) {
        content = str.slice(start, i);
      }
      depth--;
    }
  }
  return content;
}