DNA strings are constructed from the alphabet (A, C, G, T, whose symbols represe
ID: 3874786 • Letter: D
Question
DNA strings are constructed from the alphabet (A, C, G, T, whose symbols represent the bases adenine, cytosine, guanine, and thymine. As an example, "AAGATGCCGT" is a DNA string of length 10. Given two strings s and t, t is a substring of s if t is contained as a contiguous collection of symbols in s (as a result, t must be no longer than s). The position of a symbol in a string is the total number of symbols found to its left, including itself (e.g., the positions of all occurrences of 'U' in "AUGCUUCAGAAAGGUCUUACG" are 2, 5, 6, 15, 17, and 18). The symbol at position i of s is denoted by sfil. A substring of s can be represented as slj:k] where j and k represent the starting and ending positions of the substring in s; For example, if s "AUGCUUCAGAAAGGUCUUACG", then s[25] = "UGCU". The location of a substring sli-k] is its beginning position j; note that t will have multiple here: Given: Two DNA strings s and t (each of length at most 1000). Return: All locations of t as a substring of s. Sample input: GATATATGCATATACTT ATAT Sample output: 2 4 10 Please write a java program for it.Explanation / Answer
Given below is the code for the question.
Please don't forget to rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
import java.util.Scanner;
public class DNASearch {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String dnaSeq, search;
System.out.println("Enter the main DNA sequence: ");
dnaSeq = keyboard.next();
System.out.println("Enter the sequence to search: ");
search = keyboard.next();
boolean found = false;
int start = 0;
while(start < dnaSeq.length())
{
int index = dnaSeq.indexOf(search, start);
if(index == -1) //not found
break;
else
{
found = true;
System.out.print((index+1) + " "); //position is index+1
start = index + 1; //update start so next start will be after the found location
}
}
if(!found)
System.out.println(search + " was not found");
System.out.println(" Good Bye!");
}
}
output
=====
Enter the main DNA sequence:
GATATATGCATATACTT
Enter the sequence to search:
ATAT
2 4 10
Good Bye!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.