I had to list all possible 4 digit combinations from 3 given digits. Every digit should be used at least once. If numbers are (1,2,4), the option 1122 is not valid as it does not use number 4. My ugly piece of code looked like this:
String s="";
for(int i=1000;i<5000;i++){
s=String.valueOf(i);
if(s.contains("1")&&s.contains("2")&&s.contains("4")
&&!s.contains("3")&&!s.contains("5")&&!s.contains("6")&&!s.contains("7")
&&!s.contains("8")&&!s.contains("9")&&!s.contains("0")){
System.out.println(s);
}
}
And it listed 36 combinations, which is correct answer.
Can you please suggest me a regular expression that will work instead of that condition?