Last active
November 23, 2019 20:48
-
-
Save ilya3a/ddff666bc7e81c592b746a20584cf782 to your computer and use it in GitHub Desktop.
interview Q
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class DolevQ { | |
public static void main(String[] args) { | |
String s1 = "ilya"; | |
String s2 = "dolev"; | |
String s3 = "Engine"; | |
List<String> testList = new ArrayList<>(); | |
testList.add(s1); | |
testList.add(s2); | |
testList.add(s3); | |
printAllComb(testList); | |
} | |
static void printAllComb(List<String> input) { | |
int conters[] = new int[input.size()]; | |
do { | |
StringBuffer stringBuffer = new StringBuffer(); | |
for (int i = 0; i < input.size(); i++) { | |
String str = input.get(i); | |
stringBuffer.append(str.charAt(conters[i])); | |
} | |
System.out.println(stringBuffer); | |
int indexOfIncrement = getIndex(conters); | |
if (conters[indexOfIncrement] + 1 < input.get(indexOfIncrement).length()) { | |
conters[indexOfIncrement]++; | |
} else { | |
conters[indexOfIncrement] = 0; | |
if (indexOfIncrement != 0) { | |
conters[indexOfIncrement - 1]++; | |
} | |
} | |
} | |
while (!isZeroCounters(conters)); | |
} | |
private static int getIndex(int[] conters) { | |
for (int i = 0; i < conters.length; i++) { | |
if (conters[i] != 0) { | |
return i; | |
} | |
} | |
return conters.length - 1; | |
} | |
private static boolean isZeroCounters(int[] conters) { | |
for (int c : conters) { | |
if (c != 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment