重複数のある組み合わせ



package com.viking.divide;

public class DuplicateCombination {
	public static void main(String[] args) {

		int[] a = { 1, 2, 3 ,3};
		System.out.println(perm(a, 0));
	}

	public static int perm(int[] a, int begin) {

		int count = 0;
		boolean isorder = true;
		String path = "";
		for (int i = 0; i < begin - 1; i++) {
			if (a[i] > a[i + 1]) {
				isorder = false;
				break;
			}
			path += a[i] + " ";
		}
		if (isorder && begin > 0) {
			System.out.println(path + a[begin - 1]);
			count = 1;
		}
		if (begin == a.length) {
			return count;
		}

		for (int i = begin; i < a.length; i++) {
			if(isSwap(a,begin,i)){
				swap(a, begin, i);
				count += perm(a, begin + 1);
				swap(a, begin, i);
			}			
		}
		return count;
	}

	public static void swap(int[] a, int begin, int end) {
		int temp = a[begin];
		a[begin] = a[end];
		a[end] = temp;
	}
	
	public static boolean isSwap(int[] a,int begin,int end){		
		for(int i=end;i>begin;i--){
			if(a[end]==a[i-1]){
				return false;
			}
		}
		return true;	
	}
}