原题链接在这里: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. jQuery垂直缩略图相册插件 支持鼠标滑动翻页

    在线演示 本地下载

  2. Hadoop:eclipse配置hadoop-eclipse-plugin(版本hadoop2.7.3)

    配置hadoop-eclipse-plugin(版本hadoop2.7.3): 1:首先下载我们需要的  hadoop-eclipse-plugin-2.7.3.jar,winutils.exe 和 ...

  3. C++二阶构造函数

    转自:http://blog.51cto.com/9291927/1896411 一.构造函数的问题 构造函数存在问题: A.构造函数只提供自动初始化成员变量的机会 B.不能保证初始化逻辑一定成功,如 ...

  4. Oracle中清除BIN$开头的垃圾表的解决办法 [转]

    oracle drop table的时候,不会彻底删除该表,它将drop的表放到了自己的回收站里,放到回收站的表就是我们看到的形如bin$/rt62vkdt5wmrjfcz28eja==$0的表,其中 ...

  5. cmd下进入oracle sqlplus

    1.sqlplus /nolog 2.connect sys/orcl@ORCL as sysdba 3.select sysdate from dual exit;

  6. Aizu 2677 Breadth-First Search by Foxpower LCA+bfs

    A - Breadth-First Search by Foxpower Problem Statement Fox Ciel went to JAG Kingdom by bicycle, but ...

  7. bootstrap框架:常用内容一

    <!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="utf-8&q ...

  8. fiddler之使用教程(一)

    一. 什么是fiddler&它可以做什么 fiddler是位于客户端和服务器端的HTTP代理,也是目前最常用的http抓包工具之一.它能够记录客户端和服务器之间的所有HTTP请求,可以针对特定 ...

  9. 监控和审计IBM InfoSphere BigInsights和Cloudera Hadoop的访问权限

    http://www.ithov.com/server/124456.shtml 您也将学习一个仅适用于 IBM InfoSphere BigInsights 的快速启动监控实现. 大数据骚动主要集中 ...

  10. f5 ddos cc——Mitigating DDoS Attacks with F5 Technology

    摘自:https://f5.com/resources/white-papers/mitigating-ddos-attacks-with-f5-technology Mitigating Appli ...