File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+
3+ 1. 아이디어 : 두개의 StringBuilder 사용 (sorted, left)
4+ s 문자열 부터 순회하며 각 문자열 개수를 카운팅
5+ order 문자열을 순회하며, s 문자열에 order 문자열이 존재할 경우 카운팅 개수만큼 붙임 (sorted)
6+ 다시 s 문자열을 순회하며 카운팅 배열에 개수가 남아있는 경우 붙임 (left)
7+
8+ 완료 후 sorted + left 문자열을 합쳐서 반환
9+
10+ 2. 시간복잡도 : O(order 문자열 길이 + 2*s 문자열 길이) = O(N)
11+
12+ 3. 자료구조/알고리즘 : 카운팅 배열 + 완전탐색
13+
14+ */
15+
16+ class Solution {
17+ public String customSortString (String order , String s ) {
18+ // 매칭된 문자열 순서 유지
19+ // 나머지 문자 배열
20+
21+ int [] cnt = new int [26 ];
22+ StringBuilder sorted = new StringBuilder ();
23+ StringBuilder left = new StringBuilder ();
24+
25+ for (int i =0 ; i <s .length (); i ++) {
26+ cnt [s .charAt (i )-'a' ]++;
27+ }
28+
29+ for (int i =0 ; i <order .length (); i ++) {
30+ char c = order .charAt (i );
31+
32+ if (cnt [c -'a' ]>0 ) {
33+ while (cnt [c -'a' ]>0 ) {
34+ sorted .append (c );
35+ cnt [c -'a' ]--;
36+ }
37+ }
38+
39+ }
40+
41+ for (int i =0 ; i <s .length (); i ++) {
42+ char c = s .charAt (i );
43+ if (cnt [c -'a' ] <= 0 ) continue ;
44+ left .append (c );
45+ }
46+
47+ return sorted .toString () + left .toString ();
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments