-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[1859] Sorting the Sentence.java
46 lines (35 loc) · 1.08 KB
/
[1859] Sorting the Sentence.java
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
40
41
42
43
44
45
46
//Fastest Solution
class Solution {
public String sortSentence(String s) {
String[] in=s.split(" ");
String[] out=new String[in.length];
for(int i=0;i<in.length;i++){
int li=in[i].length()-1;
out[in[i].charAt(li)-48-1]=in[i].substring(0,li);
}
return String.join(" ", out);
}
}
//Slower Solution
class Solution {
public String sortSentence(String s) {
String out="";
boolean flag=true;
for(int i=1;flag;i++){
{
int spacei=0;
for(int j=0;j<s.length();j++){
flag=false;
if(s.charAt(j)==' ')spacei=j+1;
if(s.charAt(j)-48==i){
out = i==1?out+s.substring(spacei,j):out+" "+s.substring(spacei,j);
flag=true;
break;
}
System.out.println(out);
}
}
}
return out;
}
}