-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting the Sentence.java
More file actions
20 lines (20 loc) · 1 KB
/
Sorting the Sentence.java
File metadata and controls
20 lines (20 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public String sortSentence(String s) {
//splitted the sentence word by word
String[] words = s.split(" ");
//a hasmap for storing the words in order
HashMap<Integer, String> mapWords = new HashMap<>();
//creating a stringbuilder to manuplate the string
StringBuilder sMutable = new StringBuilder();
//a for loop for putting each words mapped with their corresponding order numbers in the hashmap
for(int i=0; i<words.length; i++)
mapWords.put(Integer.parseInt(words[i].substring(words[i].length()-1)), words[i].substring(0, words[i].length()-1));
//concatinating the words in their proper order to the mutable string
for(Integer i=0; i<words.length; i++)
sMutable.append(mapWords.get(i+1) + " ");
//storing the result with trimming the leading and trailing spaces if any
String result = sMutable.toString().trim();
//return the final result
return result;
}
}