Code

String list1 = "ACGCTCGATCGTAAGATGAAG";
int length1 = list1.length();
String list2 = "TTGCATATTCACAGT";
int length2 = list2.length();
String list3 = "ACGTAGCCTGCAAAGCAT";
int length3 = list3.length();

int count1 = 0;
int count2 = 0;
int count3 = 0;

for(int i=0; i<length1-2; i++){
  char[]array1 = new char[length1];
  array1 = list1.toCharArray();
  if((array1[i] == array1[i+2]) && (array1[i] != array1[i+1])){
    count1++;
  }
}
println("Line 1 has " + count1 + " occurences.");

for(int i = 0; i<length2-2; i++){
  char[]array2 = new char[length2];
  array2 = list2.toCharArray();
  if((array2[i] == array2[i+2]) && (array2[i] != array2[i+1])){
    count2++;
  }
}
println("Line 2 has " + count2 + " occurences.");

for(int i = 0; i<length3-2; i++){
  char[]array3 = new char[length3];
  array3 = list3.toCharArray();
  if((array3[i] == array3[i+2]) && (array3[i] != array3[i+1])){
    count3++;
  }
}
println("Line 3 has " + count3 + " occurences.");

0802 - Another String exercise: String pattern search

Statement:Suppose you are provided with a String which represents a sequence of base-pairs in a strand of DNA. Relax; all I'm saying is that you're given an arbitrary String which is made up entirely of a sequence of the following four letters: A,C,G,T. For example, you might receive Strings like the following:

"ACGCTCGATCGTAAGATGAAG"
"TTGCATATTCACAGT"
"ACGTAGCCTGCAAAGCAT"


Your job is to detect and count occurences of three-letter patterns with the format X*X, in which 'X' is any of the four allowed letters (A,C,G,T), and '*' is any single other letter which is not the same as X.

For example, in the three Strings above, the first String has 3 such occurrences (CGC,CTC,AGA); the second String has 4 (ATA,TAT,CAC,ACA); and the third String has none.

Write a function which takes such a String as an argument, and returns an integer reporting the number of such symmetrical 3-letter patterns in the String.

hide statement