Natacha BK
Natacha BK

Reputation: 137

All possible combinations of r elements in a given array of size n with conditions

I have this code which gives me all the possible combinations of the four elements of my array {'a', 'b', 'c', 'd'} of size 4.

Everything's working, but I need to add some specifications to my code and I can't get my head around how to do it.

The two conditions I have to add are : ‘b’ always have to be followed by ‘a’ in a string and one string can't have both the char ‘d’ and the char ‘a’.

    static void printAllKLength(char[] set, int k) { 
        int n = set.length;  
        printAllKLengthRec(set, "", n, k); 
    } 

    static void printAllKLengthRec (char[] set,  
                                   String prefix,  
                                   int n, int k) 
    { 

        if (k == 0)  {
            System.out.println(prefix); 
            return; 
        } 
        for (int i = 0; i < n; ++i) {
            String newPrefix = prefix + set[i];  
            printAllKLengthRec(set, newPrefix,  
                                    n, k - 1);  
        } 
    } 

    public static void main(String[] args) {
        char[] set1 = {'a', 'b', 'c', 'd'}; 
        int k = 4; 
        printAllKLength(set1, k); 


    } 
    } 

EDIT So, thanks to some help, I wrote this code :

    public static boolean aFollowsB(String s) {
          char[] set1 = s.toCharArray();

          for (int i = 0; i < set1.length; i++) {
            // If B is the last char, A can't possilby follow
            if (i == set1.length - 1) {
              if (set1[i] == 'b') { return false; }
            // Else if we encounter B, make sure next is an A
            } else {
              if (set1[i] == 'b') {
                if (set1[i+1] != 'a') { return false; }
              }
            }
          }

          return true;
        }

    //Création de la méthode permettant de dire qu'on ne peut avoir 'a' et 'd' dans la même string
        public static boolean hasOnlyAOrD(String s) {
          char[] set1 = s.toCharArray();

          boolean hasA = false;
          boolean hasD = false;

          for (int i = 0; i < set1.length; i++) {
            if (set1[i] == 'a') {
              hasA = true;
            } else if (set1[i] == 'd') {
              hasD = true;
            }
          }

          if (hasA && hasD) {
            return false;
          }

          return true;
        }

    //Création de la méthode printAllKLength pour imprimer toutes les strings possibles de longueur k
        static void printAllKLength(char[] set, int k) { 
            int n = set.length;  
            printAllKLengthRec(set, "", n, k); 
        } 

        static void printAllKLengthRec (char[] set,  
                                       String prefix,  
                                       int n, int k) 
        { 

            if (k == 0)  {
                System.out.println(prefix); 
                  System.out.println(prefix);
                return; 

            } 
            for (int i = 0; i < n; ++i) {
                String newPrefix = prefix + set[i];  
                printAllKLengthRec(set, newPrefix,  
                                        n, k - 1);  
            } 
        } 

        public static void main(String[] args) {
            char[] set1 = {'a', 'b', 'c', 'd'}; 
            int k = 4; 
            if (aFollowsB(set1) && hasOnlyAOrD(prefix)) {
                printAllKLength(set1, k); 
                }
}}

But it says that the methods aFollowsB and hasOnlyAorD are not applicable for the arguments...

Upvotes: 0

Views: 232

Answers (1)

sleepToken
sleepToken

Reputation: 1847

Create the following two methods - or similar methods (the logic is here).

public static boolean aFollowsB(String s) {
  char[] set = s.toCharArray();

  for (int i = 0; i < set.length; i++) {
    // If B is the last char, A can't possilby follow
    if (i == set.length - 1) {
      if (set[i] == 'b') { return false; }
    // Else if we encounter B, make sure next is an A
    } else {
      if (set[i] == 'b') {
        if (set[i+1] != 'a') { return false; }
      }
    }
  }

  return true;
}

public static boolean hasOnlyAOrD(String s) {
  char[] set = s.toCharArray();

  boolean hasA = false;
  boolean hasD = false;

  for (int i = 0; i < set.length; i++) {
    if (set[i] == 'a') {
      hasA = true;
    } else if (set[i] == 'd') {
      hasD = true;
    }
  }

  if (hasA && hasD) {
    return false;
  }

  return true;
}

And now, surround your print statement like so:

if (aFollowsB(prefix) && hasOnlyAOrD(prefix)) {
  System.out.println(prefix);
}

Upvotes: 1

Related Questions