5th row is required from the combination

Suppose that exists string s=abcd

I need a 5th string consisting of a, b, c, dthat is adbc. But I also get all the answers outside that I don't need.

So, how can I stop this method after its 5th execution?

import java.util.Arrays;
import java.util.Scanner;

class Test{
   long times;
   int n=1;
   public static void main(String[] args) {
    Test tm=new Test();
    Scanner in=new Scanner(System.in);
    int t=Integer.parseInt(in.nextLine());
    while(t!=0){
        String s=in.nextLine();
        char ch[]=s.toCharArray();          
        Arrays.sort(ch);
        String sort=String.valueOf(ch);            
        String ans;
        long n=Long.parseLong(in.nextLine());
        tm.times=n;
        tm.permu("",sort);
        t--;

    }
}

private void permu(String prefix,String str) {
    int len=str.length();          
    if(len==0){
        if(n==times){
            System.out.println(prefix);
        }
        else{
            n++;
        }          

    }
      else{
          for(int i=0;i<len;i++){
             permu(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, len));
           }
       }      
   }
}

Secondly, is there any site where I can read about permutation, combinations and probabilities for calculating and finding combinations of permutations and probabilities ... For coding, not for a mathematical thing .. and I know how to solve mathematically, but I don’t I can make the code. It is impossible to write logic for it.

+4
source share
1 answer

n . adbc.

:

if (n == times) {
    System.out.println(prefix);
    n = -1;
} else {
    if (n > -1)
        n++;
}

n == times true, prefix - adbc.

:

output

, ( void ), return; ... .

+3

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


All Articles