본문 바로가기

데일리

알고리즘연습: 특수문자제외한 문자 뒤집기2

(그전꺼보다정돈된코드)

 

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