Simple Straightforward Java Solution


  • 0
    S
    public class Solution {
        public String replaceWords(List<String> dict, String sentence) {
            String words[] = sentence.split(" ");
            StringBuilder sent = new StringBuilder();
            for(int i=0;i<words.length;i++){
                String word = words[i];
                StringBuilder sb = new StringBuilder();
                for(int j=0;j<word.length();j++){
                    sb = sb.append(word.charAt(j));
                    if(dict.contains(sb.toString())){
                        break;
                    }
                }
                sent.append(sb.toString());   
                if(i!=(words.length-1)){
                    sent.append(" ");
                }
            }
            return sent.toString();
        }
    }
    

Log in to reply
 

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