LeetCode Entry

1807. Evaluate the Bracket Pairs of a String

26.09.2026 medium 2026 kotlin rust

Replace keys in braces with values

1807. Evaluate the Bracket Pairs of a String medium substack youtube

https://dmitrysamoylenko.com/leetcode/

26.09.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1494

Problem TLDR

Replace keys in braces with values

Intuition

Find and replace.

Approach

  • we can do regex or split by braces and collect even replace odd

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(n)\)

Code

    fun evaluate(s: String, k: List<List<String>>) =
    k.associate { it[0] to it[1] }.let { m ->
        s.replace(Regex("""\((.*?)\)""")) { m[it.groupValues[1]] ?: "?" }}
    pub fn evaluate(s: String, k: Vec<Vec<String>>) -> String {
        let m: HashMap<_, _> = k.iter().map(|v| (&*v[0], &*v[1])).collect();
        s.split(['(', ')']).enumerate().map(|(i, p)| if i % 2 == 0 { p }
            else { *m.get(p).unwrap_or(&"?") }).collect()
    }

Comments