-1

I am currently learning java and wondering about some question. I am given problem that there is an n string that consist of three letter A B and C. The problem is that it can be made into a n lenght string and i need to find the numbers of combination that have three A on the n lenght string.

Any tips on how to tackle this problem? example on a 4 letter string:

ABBA AAAB ACCC BAAA and so on

thank you

SKie94
  • 1
  • _Exactly_ 3 As or _at least_ 3 As? – tobias_k Dec 20 '17 at 12:05
  • If you want _exactly_ 3 As, then there should be `(n! / 3!) * 2^(n-3)` possibilities. The first term denotes the number of possible positions for the three As, and the second term the possibilities for the remaining (n-3) characters. – tobias_k Dec 20 '17 at 13:39
  • what about at least is there any tips? – SKie94 Dec 20 '17 at 16:42

1 Answers1

0

I suppose you have an array of strings let us call it "stringsarray", then the code will be:

int result =0;
for (int i =0 ; i<stringsarray.length ; i++ ) {
  int numOfA = 0;
  for (int j =0; j<stringsarray[i].length() ; j++) {
    if (stringsarray[i].charAt(j) == 'A' ) {
       numOfA++;
    }
   }
   if (numOfA ==3) {result++};

Now the variable "result" will have the number of strings that have excatly only three ('A')s