原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/

题目:

Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and </b> to wrap the substrings in s that exist in dict. If two such substrings overlap, you need to wrap them together by only one pair of closed bold tag. Also, if two substrings wrapped by bold tags are consecutive, you need to combine them.

Example 1:

Input:
s = "abcxyz123"
dict = ["abc","123"]
Output:
"<b>abc</b>xyz<b>123</b>"

Example 2:

Input:
s = "aaabbcc"
dict = ["aaa","aab","bc"]
Output:
"<b>aaabbc</b>c"

Note:

  1. The given dict won't contain duplicates, and its length won't exceed 100.
  2. All the strings in input have length in range [1, 1000].

题解:

类似Merge Intervals. 标记出dict中每个word所在s的起始结束位置. sort后merge.

或者直接用boolean array来标记s的当前char是否出现在dict中word所在s的substring内.

Time Complexity: O(dict.length*s.length()*x). x是dict中word的平均长度.

Space: O(s.length()).

AC Java:

 class Solution {
public String addBoldTag(String s, String[] dict) {
if(s == null || s.length() == 0 || dict == null || dict.length == 0){
return s;
} boolean [] mark = new boolean[s.length()];
for(String word: dict){
for(int i = 0; i<=s.length()-word.length(); i++){
if(s.startsWith(word, i)){
Arrays.fill(mark, i, i+word.length(), true);
}
}
} int i = 0;
StringBuilder sb = new StringBuilder();
while(i<mark.length){
if(mark[i]){
sb.append("<b>");
while(i<mark.length && mark[i]){
sb.append(s.charAt(i++));
} sb.append("</b>");
}else{
sb.append(s.charAt(i++));
}
} return sb.toString();
}
}

LeetCode 616. Add Bold Tag in String的更多相关文章

  1. 【LeetCode】616. Add Bold Tag in String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  2. 616. Add Bold Tag in String加粗字符串

    [抄题]: Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b&g ...

  3. [LeetCode] Add Bold Tag in String 字符串中增添加粗标签

    Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and ...

  4. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  5. 【LeetCode】758. Bold Words in String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  6. [LeetCode] 67. Add Binary_Easy tag: String

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  7. [LeetCode] 258. Add Digits_Easy tag: Math

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  8. [LeetCode] Bold Words in String 字符串中的加粗单词

    Given a set of keywords words and a string S, make all appearances of all keywords in S bold. Any le ...

  9. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

随机推荐

  1. 【Flask】Flask Restful api

    ### 安装: Flask-Restful需要在Flask 0.8以上的版本,在Python2.6或者Python3.3上运行.通过pip install flask-restful即可安装. ### ...

  2. sublime批量替换文本重复单词

    事先需要把单词打到文本的每一行 排序 按F9或者选择菜单:Edit > Sort Lines,对每行文本进行排序 查找重复行 排序好后,按Ctrl+F,调出查找面板 查找字符串: ^(.+)$[ ...

  3. Spring_使用 JdbcTemplate和JdbcDaoSupport

  4. MySQL 触发器简单实例 - 转载

    MySQL 触发器简单实例 触发器:可以更新,删除,插入触发器,不同种类的触发器可以存在于同一个表,但同种类的不能有多个.一个更新.一个删除是可以共存的. ~~语法~~ CREATE TRIGGER  ...

  5. JDK环境变量配置问题 - 原创

    一台电脑上同时装有jdk1.7和jdk1.8,切换jdk时的报错问题 示例如下环境变量: JAVA_HOME       C:\Program Files (x86)\Java\jdk1.7 CLAS ...

  6. 文件与IO-字节输入/输出

    package IoDemo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExce ...

  7. MSSQL2005数据库显示单一用户模式,无法进行任何操作

    MSSQL2005数据库显示单一用户模式,无法进行任何操作 经查询,使用exec sp_who进行查看链接线程,发现仍然有链接不断进行请求,将链接踢出,然后通过命令修复即可恢复 处理步骤: exec ...

  8. 全方位解读Java反射(reflection)

    JAVA提供了一种反射机制,反射也称为反省. java程序运行以后内存中就是一堆对象,除了对象什么都没有. 找对象 拉关系 瞎折腾 对象在运行过程中能否有一种机制查看自身的状态,属性和行为.这就是反射 ...

  9. 公有云厂商DDoS防护产品竞品分析——内含CC的一些简单分析,貌似多是基于规则,CC策略细粒度ip/url//ua/refer

    公有云厂商DDoS防护产品竞品分析 from:http://www.freebuf.com/articles/network/132239.html 行文初衷 由于工作关系,最近接触了很多云上用户,对 ...

  10. .html() .text() .val() 的区别

    .html()用为读取和修改元素的HTML标签(包括其Html标签) .text()用来读取或修改元素的纯文本内容 (包括其后代元素) .val()用来读取或修改表单元素的value值.(只能用于表单 ...