How to get a possible 3-digit combination from a number having more than 3 digits in java

I / p: 5278
Desired o / p: 527 278 785 852 (I manually did this). but if the number is large, then this is a problem.

Note. The output should be such that there is no repeated combination (for example, in the above combination, the number 527 is enough, and I do not want to get a combination of 257 or 725 in it, etc.,

How can i do this? Any clue?

EDIT: One important input digit is unique. To be more clear at any time, the input may have a value similar to 1123 0r 3455.

+4
source share
4 answers

Here is an example:

public class Combination { public static void main(String[] args) { StringBuilder builder = new StringBuilder("5278"); String str; int lastIndex; if(builder.length() % 2 == 0) { lastIndex = builder.length() / 2; } else { lastIndex = builder.length() / 2 + 1; } str = builder.toString() + builder.toString().substring(0, lastIndex); for (int i = 0; i < builder.length(); i++) { System.out.println(str.substring(i, i + 3)); } } } 

Update easier way than higher (based on conversation with veredesmarald )

 public class Combination { public static void main(String[] args) { char[] digits = Integer.toString(123).toCharArray(); for (int i = 0; i < digits.length; i++) { System.out.println("" + digits[i] + digits[(i + 1) % digits.length] + digits[(i + 2) % digits.length]); } } } 
+3
source
 public static void printCombinations(int input) { char[] digits = Integer.toString(input).toCharArray(); for (int i = 0; i < digits.length - 2; i++) { for (int j = i + 1; j < digits.length - 1; j++) { for (int k = j + 1; k < digits.length; k++) { System.out.println("" + digits[i] + digits[j] + digits[k]); } } } } 

This will give you all the unique combinations of 3 digits from your input integer. Please note that this only works when the numbers are unique as you stated. If you need to build combinations of other lengths, you can write a recursive function.

Output Example:

 printCombinations(12345); 123 124 125 134 135 145 234 235 245 345 
+2
source
  Integer input = 5278; List<Character> digits = new ArrayList<Character>(); for(char c : String.valueOf(input).toCharArray()) { digits.add(Character.valueOf(c)); } int a = 0; for(int i=0; i<digits.size(); i++){ for(int j=0; j<digits.size(); j++){ for(int k=0; k<digits.size(); k++){ if(!digits.get(i).equals(digits.get(j)) && !digits.get(j).equals(digits.get(k)) && !digits.get(i).equals(digits.get(k))){ System.out.println(++a +": " + digits.get(i) + digits.get(j) + digits.get(k)); } } } } 
0
source

Put the input digits in the list, create a random generator that will return the indices of this list, will form your output:

 Integer input = 5278; char[] digits = String.valueOf(input).toCharArray(); int numberOfTrinities = 4; String result = ""; Random generator = new Random(); List<String> trinities = new ArrayList<String>(); while(trinities.size() < numberOfTrinities ) { String nextTrinity = ""; for(int i = 0; i < 3; i++) { nextTrinity += digits(generator.nextInt(digits.size())); } if(isUniqueDigitsTrinity(trinities, nextTrinity)) { trinities.add(nextTrinity); } } String result = ""; for(String trinity : trinities) { result += trinity; } System.out.printl("Input: " + input + ", Result: " + result); 

We do not check the unique numbers in the trinity:

 private boolean isUniqueDigitsTrinity(List<String> trinities, String candidateTrinity) { for(String existingTrinity : trinities) { String checker = existingTrinity; for(char candidateChar : candidateTrinity.toCharArray()) { checker.replace(candidateChar, ''); } if(checker.length() <= 0) { return false; } } return true; } 
-2
source

Source: https://habr.com/ru/post/1414816/


All Articles