Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69cfca90e8a0a6d4d6871c4f.md
T
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

84 lines
1.7 KiB
Markdown

---
id: 69cfca90e8a0a6d4d6871c4f
title: "Challenge 265: Deepest Brackets"
challengeType: 28
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--
`getDeepestBrackets("(hello (world))")` should return `"world"`.
```js
assert.equal(getDeepestBrackets("(hello (world))"), "world");
```
`getDeepestBrackets("[outer [inner] outer]")` should return `"inner"`.
```js
assert.equal(getDeepestBrackets("[outer [inner] outer]"), "inner");
```
`getDeepestBrackets("{a{b}c{d{e}f}g}")` should return `"e"`.
```js
assert.equal(getDeepestBrackets("{a{b}c{d{e}f}g}"), "e");
```
`getDeepestBrackets("[the {quick (brown [fox] jumped) over (the) lazy} dog]")` should return `"fox"`.
```js
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"`.
```js
assert.equal(getDeepestBrackets("f[(r)e{e}C{o[(d){e(C)}a]m}]p"), "C");
```
# --seed--
## --seed-contents--
```js
function getDeepestBrackets(str) {
return str;
}
```
# --solutions--
```js
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;
}
```