Skip to content

Commit 017c8d4

Browse files
Merge pull request #1714 from CodingTestStudy2/이진희
[이진희] Day30
2 parents 03050c0 + 1c2e258 commit 017c8d4

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
3+
1. 아이디어 : 특수문자와 소문자를 분리하여 reverse
4+
각각의 문자열을 분리해서 저장후, 문자열의 길이만큼 완전탐색을 돌며 idx를 맨 뒤 부터 탐색
5+
소문자일 경우 소문자 문자열에서, 특수문자일 경우 특수문자 문자열에서 배치
6+
7+
2. 시간복잡도 : O(N+N)
8+
9+
3. 자료구조/알고리즘 : 완전탐색, 문자열
10+
11+
*/
12+
13+
class Solution {
14+
public String reverseByType(String s) {
15+
// 특수문자 reverse
16+
// 소문자 reverse
17+
18+
StringBuilder word = new StringBuilder();
19+
StringBuilder special = new StringBuilder();
20+
21+
for(int i=0; i<s.length(); i++) {
22+
if(s.charAt(i) >= 'a' && s.charAt(i) <= 'z') word.append(s.charAt(i));
23+
else special.append(s.charAt(i));
24+
}
25+
26+
int idxWord = word.length()-1;
27+
int idxSpecial = special.length()-1;
28+
StringBuilder ans = new StringBuilder();
29+
30+
for(int i=0; i<s.length(); i++) {
31+
if(s.charAt(i) >= 'a' && s.charAt(i) <= 'z') ans.append(word.charAt(idxWord--));
32+
else ans.append(special.charAt(idxSpecial--));
33+
}
34+
35+
return ans.toString();
36+
}
37+
}

0 commit comments

Comments
 (0)