For the positionOf we search from left to right. The inner
break
means we don't waste time on impossible matches.
public class DNAString {
/** The gene. I choose to represent it as an array since its is then
more natural to access random positions and I can change it in
place without copying the whole thing (Remember: Strings are
immutable). */
protected char[] strand;
public DNAString(int length){
strand= new char[length];
}
public void set(String g){
for (int i=0; i < g.length(); i++){
strand[i] = g.charAt(i);
}
}
public String toString(){
String result = "";
for (char g : strand){
result += g;
}
return result;
}
/** @param s the substring we are looking for.
Returns the starting position of s in this DNAString or -1 if s is
not found anywhere in this strand */
public int positionOf(String s){
for (int i=0; i < strand.length - s.length(); i++){
boolean match = true;
for (int j =0; j< s.length(); j++){
if (strand[i+j] != s.charAt(j)) {
match = false;
break;
}
}
if (match) {
return i;
}
}
return -1;
}
public static void main (String [] args){
String joeDNA = "AGCTGAAAAAGTCCCCACGATCATCAAGACTTGACTACGCTAGC";
DNAString joe = new DNAString(joeDNA.length());
joe.set(joeDNA);
System.out.println(joe);
System.out.println("AAGT is first found at position " + joe.positionOf("AAGT"));
}
}
No comments:
Post a Comment