-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1859.java
More file actions
39 lines (31 loc) · 1.14 KB
/
_1859.java
File metadata and controls
39 lines (31 loc) · 1.14 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
package com.github.aditya;
import java.util.Arrays;
public class _1859 {
public static class Solution {
public String sortSentence(String s) {
String[] strArr = s.split(" ");
String[] newArr = new String[strArr.length];
for (String str : strArr) {
int i = Integer.parseInt(str.substring(str.length() - 1));
newArr[i - 1] = str.substring(0, str.length() - 1);
}
return String.join(" ", newArr);
}
}
// Bit Expensive because of Sort().
public static class Solution_1 {
public String sortSentence(String s) {
String[] strArr = s.split(" ");
String[] newArr = new String[strArr.length];
for (int i = 0; i < strArr.length; i++) {
newArr[i] = strArr[i].substring(strArr[i].length() - 1) + strArr[i].substring(0, strArr[i].length() - 1);
}
Arrays.sort(newArr);
String result = "";
for (String str : newArr) {
result = result + str.substring(1) + " ";
}
return result.trim();
}
}
}