vamsu
8/20/2018 - 9:47 PM

Combinations with list

Combinations with list

import java.io.*;
import java.util.*;

public class Solution {
    
    public static void main(String[] args) {
      Solution solution = new Solution();
      String[][] input = {
        { "John", "Emma", "Heather"},
        { "Plays", "Hates", "Watches" },
        { "NFL", "Soccer", "Chess" }
      };
      solution.printPhraseCombinations(input);
    }
  
    public void printPhraseCombinations(String[][] phrases) {
      if(phrases == null || phrases.length < 1) {
        return; 
      }

      combinations(phrases, phrases.length-1,"" );
    }
  
    private void combinations(String[][] phrases, int n, String result) {
       if(n < 0){
         System.out.println(result);
         return;
       }
       for(int i=0;i<phrases[n].length;i++){
         combinations(phrases, n-1, phrases[n][i] + " " + result);
       }
    }
}