Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2babe9b

Browse files
authored
Create Sorting the Sentence.java
1 parent f4c3051 commit 2babe9b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Easy/Sorting the Sentence.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public String sortSentence(String s) {
3+
String[] arr = new String[s.split(" ").length];
4+
int idx = 0;
5+
int n = s.length();
6+
while (idx < n) {
7+
int currIdx = idx;
8+
while (currIdx < n && Character.isLetter(s.charAt(currIdx))) {
9+
currIdx++;
10+
}
11+
int wordIdx = Character.getNumericValue(s.charAt(currIdx++));
12+
arr[wordIdx - 1] = s.substring(idx, currIdx - 1);
13+
idx = currIdx + 1;
14+
}
15+
StringBuilder sb = new StringBuilder();
16+
for (String word : arr) {
17+
sb.append(word).append(" ");
18+
}
19+
return sb.toString().trim();
20+
}
21+
}

0 commit comments

Comments
 (0)