MCPcopy Create free account
hub / github.com/ROUTINE-STUDY/Algorithm / Sanghoo

Class Sanghoo

LeetCode/Sort/1859. Sorting the Sentence/Sanghoo.java:6–30  ·  view source on GitHub ↗

https://leetcode.com/problems/sorting-the-sentence/

Source from the content-addressed store, hash-verified

4 * https://leetcode.com/problems/sorting-the-sentence/
5 */
6public class Sanghoo {
7
8 // 각 단어의 맨 끝에있는 숫자가 곧 배열 인덱스 자리라 생각하고 재배치 후 문자열 합치는 방식
9 public static String sortSentence(String s) {
10 String[] arr = s.split(" ");
11 String[] result = new String[arr.length + 1];
12
13 for(int i=0; i<arr.length; i++) {
14 String word = arr[i];
15 int len = word.length();
16 result[word.charAt(len - 1) - '0'] = word.substring(0, len-1);
17 }
18
19 StringBuilder res = new StringBuilder();
20 for(int i=1; i<result.length; i++) {
21 res.append(result[i]).append(" ");
22 }
23
24 return res.toString().trim();
25 }
26
27 public static void main(String[] args) {
28 System.out.println(sortSentence("is2 sentence4 This1 a3"));
29 }
30}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected