Java Parsing Solution


  • 1
    public static String addBoldTag(String s, String[] dict) {
        int n = s.length();
        boolean[] marked = new boolean[n];
        for (String word : dict) {
            int m = word.length();
            for (int i=0;i<=n-m;i++) 
                if (s.substring(i, i + m).equals(word)) 
                    for (int j=i;j<i+m;j++) marked[j] = true;
        }
        
        int i = 0;
        StringBuilder res = new StringBuilder();
        while (i < n) {
            if (marked[i]) {             
              int j = i;
              while (j < n && marked[j]) j++;
              res.append("<b>").append(s.substring(i,j)).append("</b>");
              i = j;                 
            }
            else res.append(s.charAt(i++));
        }
        
        return res.toString();   
        
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.