(그전꺼보다정돈된코드)
package multi_class;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class Main2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
// String str = s.nextLine();
String str = "a#b!GE*T@S";
System.out.println(solution(str));
}//end main
private static String solution(String str) {
String answer = "";
//0.리스트에 할당
List<String> list = Arrays.asList(str.split(""));
System.out.println(list);
//1.리스트의 뒷쪽에서부터 영문만 Stack에 추가
Stack<String> stack = new Stack<String>();
for (int i = list.size()-1; i >= 0; i--) {
if(list.get(i).matches("[a-zA-Z]")) {
stack.push(list.get(i));
}
}
System.out.println(stack+" :stack"); //[S, T, E, G, b, a]
//2.알파벳인 것들만 스텍에서 뽑아서 변경
for (int i = 0; i < list.size(); i++) {
// System.out.println(list.get(i).matches("[a-zA-Z]")+" :check"); //true/false로반환
if(list.get(i).matches("[a-zA-Z]")) {
list.set(i, stack.remove(0)); //remove:스택의 0번째부터 추출
}
}
// System.out.println(list); [S, #, T, !, E, G, *, b, @, a]
//3.문자열로 변환해서 할당
// answer = String.join("", list);
// answer = list.stream().collect(Collectors.joining());
answer = list.stream().reduce("", String::concat);
return answer;
}
}//end class'데일리' 카테고리의 다른 글
| 알고리즘 관련 개념 (0) | 2024.03.11 |
|---|---|
| filter 와 map의 차이 이해하기 (0) | 2024.03.08 |
| 알고리즘연습: 특수문자제외한 문자 뒤집기 (0) | 2024.03.05 |
| 알고리즘입문 자바정리 : 문자 중복배제 출력 (0) | 2024.02.27 |
| 알고리즘입문 자바정리 : 끝에서 3단어 제거 후 출력 (0) | 2024.02.27 |