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. 아이디어 : 특별한 이진 문자열을 분리하여, 가장 크도록 정렬해야 함
4+ 이때 1과 0의 개수는 같아야함, 입력은 항상 특별한 이진 문자열이 주어짐 (1 <= s.length <= 50 이지만 1은 나올 수 없음)
5+ 1100 -> (()) 식으로 생각하고, (()(())) -> ((())()) 처럼 더 크기가 큰 이진 문자열이 큰 값이 앞에 오면 된다
6+ 재귀를 활용해, 특별한 이진문자열 덩어리로 묶어, 정렬 후 최종적으로 모두 붙인 값을 반환
7+
8+ 2. 시간복잡도 : O(N^2) + O(N^2logN) => O(N^2logN)
9+
10+ 3. 자료구조/알고리즘 : 재귀, 문자열
11+
12+ */
13+
14+ class Solution {
15+ public String makeLargestSpecial (String s ) {
16+ if (s .length () == 2 ) return s ;
17+
18+ List <String > specialStr = new ArrayList <>();
19+ StringBuilder sb = new StringBuilder ();
20+ int count = 0 ;
21+
22+ for (int i =0 ; i <s .length (); i ++) {
23+ int n = s .charAt (i )-'0' ;
24+ if (n == 1 ) count ++;
25+ else count --;
26+
27+ sb .append (s .charAt (i ));
28+ if (count == 0 ) {
29+ String innoSorted = makeLargestSpecial (sb .substring (1 ,sb .length ()-1 ));
30+ specialStr .add ("1" + innoSorted + "0" );
31+ sb .setLength (0 );
32+ }
33+ }
34+
35+ Collections .sort (specialStr , Collections .reverseOrder ());
36+ StringBuilder ans = new StringBuilder ();
37+ for (String a : specialStr ) ans .append (a );
38+
39+ return ans .toString ();
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments