Skip to content

Commit b528a87

Browse files
solves sorting the sentence
1 parent 0088d02 commit b528a87

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@
450450
| 1844 | [Replace All Digits with Characters](https://leetcode.com/problems/replace-all-digits-with-characters) | [![Java](assets/java.png)](src/ReplaceAllDigitsWithCharacters.java) | |
451451
| 1848 | [Minimum Distance to the Target Element](https://leetcode.com/problems/minimum-distance-to-the-target-element) | [![Java](assets/java.png)](src/MinimumDistanceToTheTargetElement.java) | |
452452
| 1854 | [Maximum Population Year](https://leetcode.com/problems/maximum-population-year) | [![Java](assets/java.png)](src/MaximumPopulationYear.java) | |
453-
| 1859 | [Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence) | | |
453+
| 1859 | [Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence) | [![Java](assets/java.png)](src/SortingTheSentence.java) | |
454454
| 1863 | [Sum of All Subset XOR Totals](https://leetcode.com/problems/sum-of-all-subset-xor-totals) | | |
455455
| 1869 | [Longer Contiguous Segments of Ones than Zeros](https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros) | | |
456456
| 1876 | [Substrings of Size Three with Distinct Characters](https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters) | | |

src/MaximumPopulationYear.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// https://leetcode.com/problems/maximum-population-year
2+
// T: O(N)
3+
// S: O(1)
4+
15
public class MaximumPopulationYear {
26
public int maximumPopulation(int[][] logs) {
37
final int[] population = new int[101];

src/SortingTheSentence.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class SortingTheSentence {
5+
public String sortSentence(String s) {
6+
final List<Word> words = getWords(s);
7+
words.sort(Word::compareTo);
8+
final StringBuilder result = new StringBuilder();
9+
for (Word word : words) {
10+
result.append(word.val).append(' ');
11+
}
12+
return result.deleteCharAt(result.length() - 1).toString();
13+
}
14+
15+
private List<Word> getWords(String s) {
16+
StringBuilder word = new StringBuilder();
17+
final List<Word> words = new ArrayList<>();
18+
for (int index = 0 ; index < s.length() ; index++) {
19+
if (Character.isDigit(s.charAt(index))){
20+
words.add(new Word(word.toString(), s.charAt(index) - '0'));
21+
word = new StringBuilder();
22+
} else if (s.charAt(index) != ' '){
23+
word.append(s.charAt(index));
24+
}
25+
}
26+
return words;
27+
}
28+
29+
private static final class Word implements Comparable<Word> {
30+
private final String val;
31+
private final int position;
32+
33+
private Word(String val, int position) {
34+
this.val = val;
35+
this.position = position;
36+
}
37+
38+
@Override
39+
public int compareTo(Word o) {
40+
return Integer.compare(this.position, o.position);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)