본문 바로가기
Programming/>> Algorithm

[Lucky Algorithm] Super Reduced String

by 니키ᕕ( ᐛ )ᕗ 2017. 10. 4.

이제 warmup 끝났으니 랜덤모드로 하려고 했는데.. 랜덤으로 풀기 누르니까 Expert 등급이 떠서... 목차별로 하나씩 풀기로 변경..ㅠㅠ


Steve has a string, , consisting of  lowercase English alphabetic letters. In one operation, he can delete any pair of adjacent letters with same value. For example, string "aabcc" would become either "aab" or "bcc" after operation.

Steve wants to reduce  as much as possible. To do this, he will repeat the above operation as many times as it can be performed. Help Steve out by finding and printing 's non-reducible form!

Note: If the final string is empty, print Empty String .

Input Format

A single string, .

Constraints

Output Format

If the final string is empty, print Empty String; otherwise, print the final non-reducible string.


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String super_reduced_string(String s){
        // Complete this function
        int idx = 1;
        while(idx < s.length()){
            if(s.charAt(idx) == s.charAt(idx-1)){
                s = s.replaceAll(s.substring(idx-1, idx+1), "");
                idx = 0;
            }
            idx++;
        }
        if(s.length() == 0) s = "Empty String";
        return s;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        String result = super_reduced_string(s);
        System.out.println(result);
    }
}


'Programming > >> Algorithm' 카테고리의 다른 글

[Lucky Algorithm] Big Sorting  (0) 2017.10.04
[Lucky Algorithm] Time Conversion  (0) 2017.10.04
[Lucky Algorithm] Birthday Cake Candles  (0) 2017.10.03
[Lucky Algorithm] Staircase  (0) 2017.10.03
[Lucky Algorithm] Plus Minus  (0) 2017.10.03

댓글