Courses‎ > ‎AP Computer Science 2‎ > ‎konstantinovich‎ > ‎

2018-02-08

posted Feb 8, 2018, 8:31 PM by Konstantinovich Samuel   [ updated Feb 9, 2018, 5:23 AM ]

Results from discussion in class about generating all possible n-letter combinations:

public static ArrayList<String> combinate(int n){
    ArrayList<String> ans = new ArrayList<>();
    combinateH(n,"",ans);
    return ans;
  }

  public static void combinateH(int n, String word, ArrayList<String>ans){
    if(word.length() == n){
      ans.add(word);
    }else{
      for(char c = 'A'; c <= 'Z'; c++){
        combinateH(n,word+c,ans);
      }
    }
  }



  public static void main(String[]args){
      System.out.println(combinate(5).size()+ "==" + Math.pow(26,5));
  }





Comments