Posts Add Bold Tag in String
Post
Cancel

Add Bold Tag in String

PROBLEM DESCRIPTION

You are given a string s and an array of strings words.

You should add a closed pair of bold tag <b> and </b> to wrap the substrings in s that exist in words.

  • If two such substrings overlap, you should wrap them together with only one pair of closed bold-tag.
  • If two substrings wrapped by bold tags are consecutive, you should combine them.

Return s after adding the bold tags.

leetcode

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {

    public String addBoldTag(String s, String[] words) {

        // Create a boolean array 'bold' to mark which characters in 's' should be bolded.
        boolean[] bold = new boolean[s.length()];

        // Iterate through the array of words to find and mark their occurrences in 's'.
        for (int i = 0; i < words.length; i++) {

            String word = words[i];

            // Find the first occurrence of 'word' in 's'.
            int idx = s.indexOf(word);

            // mark all positions corresponding to the current word in string s as bold
            while (idx != -1) {
                for (int j = idx; j <= idx + word.length() - 1; j++) {
                    bold[j] = true;
                }

                // Find the next occurrence of 'word' in 's'.
                idx = s.indexOf(word, idx + 1);
            }
        }

        String openTag = "<b>";
        String closeTag = "</b>";

        // answer string
        StringBuffer sb = new StringBuffer();

        // go through each position to check if it should be bold
        for (int i = 0; i < bold.length; i++) {

            if (bold[i] == true) {

                // If the current character should be bolded and it's the start of a new bolded sequence.
                if (i == 0 || bold[i - 1] == false) {
                    sb.append(openTag);
                }

            }

            // Append the current character to the result.
            sb.append(s.charAt(i));

            if (bold[i] == true) {

                // If the current character should be bolded and it's the end of a bolded sequence.
                if (i == bold.length - 1 || bold[i + 1] == false) {
                    sb.append(closeTag);
                }

            }
        }

        return sb.toString();
    }
}
This post is licensed under CC BY 4.0 by the author.