Short java code


  • 0
    P
    class Solution {
        public String replaceWords(List<String> dict, String sentence) {
            String [] str = sentence.trim().split("\\s+");
            for(int i=0 ; i < dict.size(); i++){
               for(int j=0; j<str.length;j++){
                   if(str[j].indexOf(dict.get(i)) ==0){
                       str[j]=dict.get(i);}}}
           String res="";
           for(String s : str){
                res+=s+" ";
            }return res.trim();
        }
    }
    
    
    

Log in to reply
 

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