-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingSentence.java
More file actions
27 lines (23 loc) · 920 Bytes
/
SortingSentence.java
File metadata and controls
27 lines (23 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package solutions;
// [Problem] https://leetcode.com/problems/sorting-the-sentence
class SortingSentence {
// O(n) time, O(n) space
public String sortSentence(String s) {
String[] words = s.split(" ");
String[] sortedWords = new String[words.length];
for (String word: words) {
int orderIndex = word.length() - 1;
int wordIndex = word.charAt(orderIndex) - '1';
sortedWords[wordIndex] = word.substring(0, orderIndex);
}
return String.join(" ", sortedWords);
}
// test
public static void main(String[] args) {
SortingSentence solution = new SortingSentence();
String input = "is2 sentence4 This1 a3";
String expectedOutput = "This is a sentence";
String actualOutput = solution.sortSentence(input);
System.out.println("Test passed? " + expectedOutput.equals(actualOutput));
}
}