자바 문자열에서 순열 뽑아내기

조회수 2122회

abc를 집어넣었을 때 abc acb bca bac cab cba 가 나오는 코드를 짜봤는데 제대로된 결과가 안나오네요. 코드가 어떻게 잘못되었고 어떻게 고쳐야 하는지 알 수 있을까요?

//죄송합니다. 완성하지 못했습니다....

import java.util.Arrays;

public class HW0 { public static String [] result; public static int size; public static int a=0;

private static String[] permutation(String prefix, String input) {
    size = 1;
    for(int i=0; i<input.length();i++)
    {
       size = size * (input.length()-i);
    }
    result = new String[size];

    if (input.isEmpty()) {
      result[a]=prefix;
      a++;

    }else{
        for(int i=0; i<input.length(); i++){
            permutation(prefix+input.charAt(i),
                    input.substring(0, i)+input.substring(i+1,input.length()));

            }
    }


    Arrays.sort(result);
    return result;
}


public static String[] printPermutations(String s)
{
    String[] b = permutation("",s);
    return b;

    /*
    You should return a list of strings populated by the unique permutations
    of the input, s, in alphabetical order.
    */

}

/*
Do not edit anything below this comment.
*/

public static void main(String[] args)
{
    String[] permutations = printPermutations(args[0]);
    for(String p : permutations)
    {
        System.out.println(p);
    }
}

}

1 답변

  • 좋아요

    1

    싫어요
    채택 취소하기

    숙제는 스스로 하셔야 하는데 ㅎㅎ

    변수를 초기화 하는 부분이 여러번 실행되어서 매번 새로 초기화 되서 그렇습니다.

        size = 1;
        for(int i=0; i<input.length();i++)
        {
           size = size * (input.length()-i);
        }
        result = new String[size];
    

    아래처럼 초기화를 1번만 하도록 수정하시면 됩니다.

    class Perm {
      public static String [] result;
      public static int size;
      public static int a=0;
    
      private static String[] permutation(String prefix, String input) {
        if (input.isEmpty()) {
          result[a]=prefix;
          a++;
        } else{
          for(int i=0; i<input.length(); i++){
            permutation(prefix+input.charAt(i),
                        input.substring(0, i)+input.substring(i+1,input.length()));
          }
        }
    
        return result;
      }
    
      public static String[] printPermutations(String s) {
        size = 1;
        for (int i=0; i<s.length();i++) {
          size = size * (s.length()-i);
        }
    
        result = new String[size];
    
        String[] b = permutation("",s);
        return b;
    
        /*
          You should return a list of strings populated by the unique permutations
          of the input, s, in alphabetical order.
        */
    
      }
    
      /*
        Do not edit anything below this comment.
      */
    
      public static void main(String[] args) {
        String[] permutations = printPermutations(args[0]);
        for(String p : permutations) {
          System.out.println(p);
        }
      }
    
    }
    
    • (•́ ✖ •̀)
      알 수 없는 사용자
    • 감사합니다. 과제이긴 한데 이미 기한이 끝난 과제라서요... 궁금증에 여쭤봤습니다. 감사합니다 ^^ Yong Suk Kim 2016.9.10 23:11
    • 옙~ 좋은 개발자로 성장하시기를! 알 수 없는 사용자 2016.9.11 00:26

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)