616. Add Bold Tag in String
阿新 • • 發佈:2018-11-09
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, iftwo 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" // solution 1 :// the solution that uses boolean array is kinda smart https://leetcode.com/problems/add-bold-tag-in-string/discuss/104248/Java-Solution-boolean-array // not my code Use a boolean array to mark if character at each position is bold or not. After that, things will become simple. Input: s = "abcxyz123" dict= ["abc","123"] Output: "<b>abc</b>xyz<b>123</b>" // The method startsWith() is used for checking prefix of a String. It returns a boolean value true or // // false based on whether the specified string is prefix of the particular String or not. //boolean startsWith(String str): It returns true if the str is a prefix of the String. // boolean startsWith(String str, index fromIndex): It returns true if the String begins with str, it // // starts looking from the specified index “fromIndex”. // https://beginnersbook.com/2013/12/java-string-startswith-method-example/ public class Solution { public String addBoldTag(String s, String[] dict) { boolean[] bold = new boolean[s.length()]; for (int i = 0, end = 0; i < s.length(); i++) { for (String word : dict) { if (s.startsWith(word, i)) { end = Math.max(end, i + word.length()); } } bold[i] = end > i; } StringBuilder result = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (!bold[i]) { result.append(s.charAt(i)); continue; } int j = i; while (j < s.length() && bold[j]) j++; result.append("<b>" + s.substring(i, j) + "</b>"); i = j - 1; } return result.toString(); } } // sol 2 : similar to merge intervals https://leetcode.com/problems/add-bold-tag-in-string/discuss/104263/Java-solution-Same-as-Merge-Interval // indexOf() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf str.indexOf(searchValue[, fromIndex]) Parameters Section searchValue A string representing the value to search for. fromIndex Optional An integer representing the index at which to start the search; the default value is 0. For fromIndex values lower than 0 or greater than str.length, the search starts at index 0 and str.length respectively. Return value Section The index of the first occurrence of searchValue, or -1 if not found. An empty string searchValue will match at any index between 0 and str.length Description Section Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character of a string called stringName is stringName.length - 1. 'Blue Whale'.indexOf('Blue'); // returns 0 'Blue Whale'.indexOf('Blute'); // returns -1 'Blue Whale'.indexOf('Whale', 0); // returns 5 'Blue Whale'.indexOf('Whale', 5); // returns 5 'Blue Whale'.indexOf('Whale', 7); // returns -1 'Blue Whale'.indexOf(''); // returns 0 'Blue Whale'.indexOf('', 9); // returns 9 'Blue Whale'.indexOf('', 10); // returns 10 'Blue Whale'.indexOf('', 11); // returns 10 The indexOf() method is case sensitive. For example, the following expression returns -1: 'Blue Whale'.indexOf('blue'); // returns -1 // thoughts class Solution { public String addBoldTag(String s, String[] dict) { // step 1 : check which substring is in the dict, if it's in the dict, get the index pair // step 2 : after collected all the index pair, do a summary (merge interval) on them // step 3 : get getting the final summary of the index pair, we can add bold tags on them now // step 4 : return } } // check if the subtring that need bold tags are overllaping with each other // check if they are consecutive // if they are overlapping and consecutive , only need to find their start and end , and only add one // pair of bold tags // i kinda understand why this question is tagged as related to merge intervals becuase , the processing pf checking if substring are overlappiong and conseutice it's like chekcing if their index are overlapping or next to each other // first need to find whoch substring is in the dict, and get their start , end index , merge all the substirng from the dict and in the string 's start and end index , so we have a summary on the start and end index , and we can efficnetly add bold tags Input: s = "abc xyz 123" dict = ["abc","123"] Output: "<b>abc</b>xyz<b>123</b>" // this simple example, abs's start and end index, 123's start and end index // use a stringbuilder // how to get the start and end index of the final result Input: 0123456 s = "aaabbcc" dict = ["aaa","aab","bc"] Output: "<b>aaabbc</b>c" aaa (0, 2) aab(1, 3) bc(4, 5) // after get the index pairs, do a merge intervals on them , finally we will get (0, 5) // which is aaabbc <b>aaab<b> <b>bc<b>c 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.