0

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?

o_petriv
  • 68
  • 6
  • I don't undestand the first sentence. Do you mean all possible 4-digit numbers combined from 3 given digits? In your second sentence you probably mean "digit" instead of "number", too, I guess. – Hubert Grzeskowiak Jun 25 '17 at 09:06
  • 3
    I don't know whether this is possible through regex (since your length of string // 4 is more than the number of digits allowed // 3); but, even if it is, then it would be a very poor idea to do so, IMO. – Am_I_Helpful Jun 25 '17 at 09:10
  • Your code does not do what you tell it should do. The code looks for all digits in the number, which is not possible as you only go through 1000..5000. – Hubert Grzeskowiak Jun 25 '17 at 09:10
  • 3
    I second what @Am_I_Helpful said. This question basically reads like "How to open a glass of jam with a hammer?" – Hubert Grzeskowiak Jun 25 '17 at 09:11
  • 1
    [Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. - Jamie Zawinski](https://en.wikiquote.org/wiki/Jamie_Zawinski) – Andreas Jun 25 '17 at 09:49
  • I have corrected the formulation of the question. I go only until 5000 because the largest possible combination of those digits is 4421. – o_petriv Jun 25 '17 at 10:43

2 Answers2

1

The possible combinations are:

1241
1242
1244

I simply used 3 of the required digits (1, 2, 4) which leaves us with only one digit to fill. Since we have 3 digits to choose from, there is only three possible combinations.

To get all permutations, shuffle around all the digits in each number and eliminate duplicates. There is plenty of articles about how to get permutations, e.g. Permutation of array. To eliminate duplicates you can use a Java Set.

Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
0

The following regex would test that your numbers are 4 digits long, composed of 1, 2 and 4 with at least one occurrence of each, using lookaheads :

^(?=.*1)(?=.*2)(?=.*4)[124]{4}$

A solution without using lookaheads would be possible but also extremely uninteresting as you would have to list the third of every permutation (you would only gain the opportunity to describe that one specific spot can contain either of the three symbols).

As others said, using this regex with your current code would still be a very inefficient way to generate your desired permutations.

Aaron
  • 24,009
  • 2
  • 33
  • 57