LeetCode 616. Add Bold Tag in String
原题链接在这里: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:
- The given dict won't contain duplicates, and its length won't exceed 100.
- 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的更多相关文章
- 【LeetCode】616. Add Bold Tag in String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 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 ...
- [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 ...
- [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 ...
- 【LeetCode】758. Bold Words in String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- [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 ...
- [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. ...
- [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 ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
随机推荐
- 【Java】Swing+IO流实现一个简单的文件加密程序(demo版)
留着参考 EncrytService package com.my.service; import java.io.File; import java.io.FileInputStream; impo ...
- gh-ost测试
gh-ost测试 1.不支持没有主键或者唯一索引的表 2018-08-24 09:53:33 FATAL No PRIMARY nor UNIQUE key found in table! Baili ...
- Sybase:解锁
Sybase:解锁 Sql代码: --查询锁表 sp_iqlocks --解除锁定 drop connection[连接序号]
- JavaWeb Listener
1. 监听器概述 1.1. 什么是监听器 做过Swing或者AWT图像界面Java程序开发的话,应该对Listener与Event非常熟悉.Swing或者AWT中通过Listener与Event来处理 ...
- 华为交换机S5700系列配置通过STelnet登录设备示例
配置通过STelnet登录设备示例 组网图形 图1 配置用户通过STelnet登录设备组网图 在服务器端生成本地密钥对 <HUAWEI> system-view [HUAWEI] sysn ...
- Python 之 matplotlib (十六)Animation动画【转】
本文转载自:https://blog.csdn.net/wangsiji_buaa/article/details/80057875 代码: import matplotlib.pyplot as ...
- Servlet容器初始化IOC容器
<!-- ServletContext参数,配置Ioc容器的xml文件名 --> <context-param> <param-name>contextConfig ...
- Merge-Sort(归并排序)
Merge-Sort(归并排序) 思想 利用分治的思想,具体实现也就是递归,不断的将问题话分为更小的子问题,当子问题中规模为1的时候,认为数组已经有序了,然后再将子问题求得的结果不断的合并.也就是将长 ...
- angular custom Element 自定义web component
angular 自定义web组件: 首先创建一个名为myCustom的组件. 引入app.module: ... import {customComponent} from ' ./myCustom. ...
- idea的常用设置
1.官网 官网:http://www.jetbrains.com/idea/download/#section=windows 官方文档:http://www.jetbrains.com/help/i ...