【每日一题Day85】LC1807 替换字符串中的括号内容 | 哈希表 双指针

简介: 如果当前字符不是左括号,那么将其直接放入结果末尾;如果是左括号,那么搜索括号内的单词,然后进行替换。

替换字符串中的括号内容【LC1807】


You are given a string s that contains some bracket pairs, with each pair containing a non-empty key.


  • For example, in the string "(name)is(age)yearsold", there are two bracket pairs that contain the keys "name" and "age".


You know the values of a wide range of keys. This is represented by a 2D string array knowledge where each knowledge[i] = [keyi, valuei] indicates that key keyi has a value of valuei.


You are tasked to evaluate all of the bracket pairs. When you evaluate a bracket pair that contains some key keyi, you will:


  • Replace keyi and the bracket pair with the key’s corresponding valuei.
  • If you do not know the value of the key, you will replace keyi and the bracket pair with a question mark "?" (without the quotation marks).


Each key will appear at most once in your knowledge. There will not be any nested brackets in s.


Return the resulting string after evaluating all of the bracket pairs.


不看题目 直接看示例更快


  • 思路:


。为了快速替换,使用哈希表存储键值和对应的value值。

。如果当前字符不是左括号,那么将其直接放入结果末尾;如果是左括号,那么搜索括号内的单词,然后进行替换。

。替换规则为:如果哈希表内存在该单词,则替换为value值,不存在则替换为"?"


  • 思路


class Solution {
    public String evaluate(String s, List<List<String>> knowledge) {
        int n = s.length();
        Map<String,String> map = new HashMap<>();
        for (List<String> list : knowledge){
            map.put(list.get(0), list.get(1));
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++){
            if (s.charAt(i) != '('){
                sb.append(s.charAt(i));
            }else{
                int j = i + 1;
                while(s.charAt(j) != ')'){
                    j++;
                }
                sb.append(map.getOrDefault(s.substring(i + 1, j), "?"));
                i = j;
            }
        }
        return sb.toString();
    }
}


。复杂度


  • 时间复杂度:O ( n ) ,n为字符串长度
  • 空间复杂度:O ( m ) ,m为数组的大小
目录
相关文章
|
1月前
|
算法 C语言
OJ刷题:求俩个数组的交集(没学哈希表?快排双指针轻松搞定!)
OJ刷题:求俩个数组的交集(没学哈希表?快排双指针轻松搞定!)
|
4月前
|
存储
【每日一题Day132】LC23633合并相似的物品 | 哈希表 排序+双指针
【每日一题Day132】LC23633合并相似的物品 | 哈希表 排序+双指针
28 0
|
1月前
|
算法 索引
LeetCode刷题--- 138. 复制带随机指针的链表(哈希表+迭代)
LeetCode刷题--- 138. 复制带随机指针的链表(哈希表+迭代)
|
3月前
|
Go Python Java
Python每日一练(20230404) Pow(x, n)、括号生成、填充每个节点的下一个右侧节点指针 II
Python每日一练(20230404) Pow(x, n)、括号生成、填充每个节点的下一个右侧节点指针 II
20 0
Python每日一练(20230404) Pow(x, n)、括号生成、填充每个节点的下一个右侧节点指针 II
|
4月前
【每日一题Day303】统计点对的数目 | 哈希表+双指针
【每日一题Day303】统计点对的数目 | 哈希表+双指针
22 0
|
4月前
|
索引
【每日一题Day281】LC142链表 Ⅱ| 快慢指针 哈希表
【每日一题Day281】LC142链表 Ⅱ| 快慢指针 哈希表
15 0
|
4月前
|
存储 索引
【每日一题Day280】LC141环形链表 | 快慢指针 哈希表
【每日一题Day280】LC141环形链表 | 快慢指针 哈希表
18 0
【每日一题Day49】LC1775通过最少操作数使数组和相等 | 贪心 + 哈希表 + 双指针
为了快速获得数组nums1和nums2的最大值和最小值,可以使用哈希表统计每个数字出现的次数map1、map2,倒序遍历map1获得max1,正序遍历map2获得min2【也可以将数组排序】
54 0
|
存储 索引
【题型总结】使用双指针+哈希表寻找符合某种条件的字符串/数字的个数
记录第一出现的字符的位置和最后一个出现的字符的位置,如果相减长度为s1的长度,那么证明是连续出现的子串
68 0
【每日一题Day48】LC1805字符串中不同整数的数目 | 双指针+哈希表
思路:使用双指针定位字符串中整数的起始位置和结束位置,去除前导0后,将该整数放入哈希表中,最后返回哈希表的大小即可。
59 0