image_2021-11-07_04-26-24.png
44 KB
#N1859. Sorting the Sentence
problem link
#solution
problem link
#solution
class Solution {
public String sortSentence(String s) {
String[] words=s.split(" ");
String[] sorted=new String[words.length];
String res="";
for(String word:words){
sorted[Character.getNumericValue(word.charAt(word.length()-1))-1]=word.substring(0, word.length()-1);
}
for(int i=0; i<sorted.length; i++){
res+=sorted[i];
if(i!=sorted.length-1)
res+=" ";
}
return res;
}
}