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没有指针
先说结论:java没有指针,它使用对象引用来替代指针 备注:c/c++的引用和java的引用完全不是一个东西 c/c++的引用是同一块内存的不同名字 java的引用指向一个对象,引用本身也占用了内存 ...
- php上传文件后无法移动到指定目录的解决
从浏览器访问而触发PHP脚本运行的用户是 apache 用户 无法移动文件的原因主要是目标目录没有写入权限 1.将目标目录权限设置为 777 #chmod 777 tar_dir 2.将目标目录用户和 ...
- Linux7关闭防火墙
RedHat Enterprise Linux 7关闭防火墙方法 在之前的版本中关闭防火墙等服务的命令是 service iptables stop /etc/init.d/iptables stop ...
- Logback Pattern 日志格式配置
Logback日志配置示例 <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppe ...
- poj2349 Arctic Network - 最小生成树
2017-08-04 16:19:13 writer:pprp 题意如下: Description The Department of National Defence (DND) wishes to ...
- CocoaPods学习系列1——安装和常规使用
CocoaPods是一个Github上的开源项目,目前已经成为iOS开发过程中标准的依赖库管理器,提供了一种对第三方类库简单优雅的集成和管理方案. 其工作原理,是将第三方类库统一管理到一个名为Pods ...
- Struts2小例子
第一个Struts 2.0例子 工具:MyEclipse 6.0.1 第一步:新建web project 第二步:为项目加入Struts 2.0 的jar包 官方下载地址:http://struts. ...
- Jni_Linux_01_转
1.Linux下JNI的使用(http://www.cnblogs.com/bastard/archive/2012/05/17/2506877.html) Linux下 JNI的使用 学习Andro ...
- CSS设置小技巧
水平居中 对于元素的水平居中,有三种情况: 行内元素(文字.图片等):text-align: center; 定宽块状元素(有设置宽度的block元素):margin: 0 auto; 不定宽块状元素 ...
- Angular 2 Architecture Overview
Module 简单来说模块(module)就是完成共同目的的代码块,export一些内容例如一个类.函数.或值变量. component就是一个基本的Angular块,一个component类其实也是 ...