[プログラマ](Java)-かっこを変換


質問リンク


https://programmers.co.kr/learn/courses/30/lessons/60058

問題を解く


問題をよく読みながら行います.
vの場合、再帰的に返すべきだと思いますので、dfsを使用してアクセスするべきです.
まず文字列をu,vに分ける.
任意の変数cnt(入力--,)を入力すると、++は、再び0になった瞬間にバランスの取れた括弧文字列となり、その瞬間に中断し、直前の文字列u,残りの文字列vとなる.
    public static String [] seperate(String p){
        String [] result = {"",""}; //String[0] = u, String[1] =v;
        int cnt =0;
        for(int i=0; i<p.length(); i++){
            if(p.substring(i,i+1).equals("(")){
                cnt ++;
            }else{
                cnt --;
            }
            result[0] +=p.substring(i,i+1);
            if(cnt==0){
                result[1] = i==p.length()-1 ? "" : p.substring(i+1,p.length());
                break;
            }
        }
        return result;
    }
得られた文字列uについて、正しい文字列であるか否かを確認する.
正しい文字列になるためには、より早く現れないようにしなければなりません.
だから(出てくるときは++、)出てくるときは--、0より小さい(出てくるときは++、)を表すと(もっと早く出てくるという意味なので、正しくない文字列です).
    public static boolean chk(String p){ 
        boolean res = true;
        int cnt =0;
        for(int i=0; i<p.length(); i++){
            if(p.substring(i,i+1).equals("(")){
                cnt ++;   
            }else{
                cnt --;
            }
            if(cnt<0){
                res = false;
            }
        }
        return res;
    }
uが正しい文字列であれば、結果にuを加え、vに対して再び再帰する.
if(chk(result[0])){
                //u가 올바른 문자열
                answer += result[0];
                answer+=dfs(result[1]);
            }
文字列が無効な場合は、
else{
                //올바른 문자열이 아닌경우
                //4-1
                String temp = "(";
                //4-2
                temp+=dfs(result[1]);
                //4-3
                temp+=")";
                //4-4
                temp+=revers(result[0]);
                //4-5
                answer+=temp;
            }
ぐるっと回った.
段階的に進める.
4~4フェーズの場合は、(->)、)->(に変換されます.
    public static String revers(String p){
        String res = "";
        p = p.substring(1,p.length()-1);
        if(p.length()==0){
            return "";
        }else{
            for(int i=0; i<p.length(); i++){
                if(p.substring(i,i+1).equals("(")){
                    res+=")";
                }else{
                    res+="(";
                }
            }
        }
        return res;
    }
dfsの開始文字が「」の場合、すぐに戻ります.
        if(p.equals(answer)){
            return answer;
        }

コード#コード#

import java.util.*;

class Solution {
    public String solution(String p) {
        String answer = "";
        
        answer = dfs(p);
        //System.out.println(Arrays.toString(result));
        
        return answer;
    }
    public static String dfs(String p){
        String answer ="";
        if(p.equals(answer)){
            return answer;
        }
    
            String [] result = seperate(p);
            //System.out.println(Arrays.toString(result));
            if(chk(result[0])){
                //u가 올바른 문자열
                answer += result[0];
                
                //p = result[1];
                answer+=dfs(result[1]);
            }else{
                //올바른 문자열이 아닌경우
                //4-1
                String temp = "(";
                //4-2
                temp+=dfs(result[1]);
                //4-3
                temp+=")";
                //4-4
                temp+=revers(result[0]);
                //4-5
                answer+=temp;
            }

        return answer;
    }
    
    public static String [] seperate(String p){
        
        String [] result = {"",""}; //String[0] = u, String[1] =v;
        
        
        int cnt =0;
        for(int i=0; i<p.length(); i++){
            if(p.substring(i,i+1).equals("(")){
                cnt ++;
               
            }else{
                cnt --;
            }
            result[0] +=p.substring(i,i+1);
            if(cnt==0){
                result[1] = i==p.length()-1 ? "" : p.substring(i+1,p.length());
                break;
            }
        }
        
        
        return result;
    }
    
    public static boolean chk(String p){
        
        boolean res = true;
        int cnt =0;
        for(int i=0; i<p.length(); i++){
            
            if(p.substring(i,i+1).equals("(")){
                cnt ++;   
            }else{
                cnt --;
            }
            
            if(cnt<0){
                res = false;
            }
        }
        
        return res;
    }
    
    public static String revers(String p){
        String res = "";
        p = p.substring(1,p.length()-1);
        if(p.length()==0){
            return "";
        }else{
            for(int i=0; i<p.length(); i++){
                if(p.substring(i,i+1).equals("(")){
                    res+=")";
                }else{
                    res+="(";
                }
            }
        }
        return res;
    }
}