A Way For Learning

Program - Java

No comments
As you may know, in DNA there are four bases — 
adenine (A), 
cytosine (C), 
guanine (G) and 
thymine (T). 
Typically A bonds with T and C bonds with G, forming the "rungs" of the DNA double helix structures.
We define the complement of a base to be the base it bonds to — i.e. the complement of A is T, the complement of T is A, the complement of C is G and the complement of G is C.
We can also define the complement of a DNA string to be the string with each base complemented. e.g. the complement of GATATC is CTATAG and then reverse the obtained string which is equal to the given string or some portion of the reversed obtained string can be a part of substring in the given string (Check for the example). We call such strings a reverse palindrome or complemented palindrome.
ACATATATAGACT is the given string.
TGTATATATCTGA is the complemented string.
AGTCTATATATGT is the reverse of the complemented string.
ATATAT            First Sub String.
TATATA            Second Sub String.
The above two strings are substrings of the reverse complemented string which in turn is a part of the given string.

Input:
You will be given a single string consisting of only the characters ACGT in upper case.
Output:
Your program should output the longest reverse palindromic substring from the given input string, If multiple solutions exists, then you need to print all of them in lexicographic order. Don't print duplicates.
Note:
If no substrings found, simply output a message "No reverse palindromic substrings available" without Quotes

Sample Input: 1
ACATATATAGACT
Sample Output: 1
ATATAT
TATATA

Sample Input: 2
ACTG
Sample Output: 2
No reverse palindromic substrings available
Sample Input: 3
GATC
Sample Output : 3
GATC
-----------------------------------
import java.util.Scanner;
    2 import java.util.*;
    3 class StringMethods{
    4  public String complement(String first){
    5   int len=first.length();
    6   char[] chars = first.toCharArray();
    7   String comp="";
    8   for(int i=0;i<len;i++){
    9    if(chars[i]=='A'){
   10     comp=comp+"T";
   11    }else if(chars[i]=='C'){
   12     comp=comp+"G";
   13    }else if(chars[i]=='T'){
   14     comp=comp+"A";
   15    }else if(chars[i]=='G'){
   16     comp=comp+"C";
   17    }
   18   }
   19   return  comp;
   20  }
   21 
   22 public String reverse(String second){
   23  String rev= "";
   24  char[] chars = second.toCharArray();
   25  for(int i=second.length()-1;i>=0;i--){
   26   //System.out.println(chars[i]);
   27   rev=rev+chars[i];
   28  }
   29  return rev;
   30 
   31 }
   32 public ArrayList<String> subString(String third,String input){
   33  ArrayList<String> wordArrayList = new ArrayList<String>();
   34  ArrayList<String> wordArrayList1 = new ArrayList<String>();
   35  int lenrev = third.length();
   36  int inputLen=input.length();  
   37     for( int c = 0 ; c < lenrev ; c++ )
   38       {
   39          for( int i = 1 ; i <= lenrev - c ; i++ )
   40          {
   41             String sub = third.substring(c, c+i);
   42             int subLen=sub.length();
   43             
   44             if(subLen>=4){
   45              if(input.contains(sub)){
   46               wordArrayList.add(sub);
   47              }
   48          }
   49             
   50         }
   51     }
   52     int max=0;
   53     for(int i=0;i<wordArrayList.size();i++){
   54      String each=wordArrayList.get(i);
   55      if(max<each.length()){
   56       max=each.length();
   57      }
   58     }
   59     //System.out.println(max);
   60     for(int k=0;k<wordArrayList.size();k++){
   61      String each=wordArrayList.get(k);
   62      if(max==each.length()){
   63       wordArrayList1.add(each);
   64      }
   65 
   66     }
   67 return wordArrayList1;
   68 }
   69 
   70 }
   71 
   72 public class pc{
   73  public static void main(String[] args) {
   74   Scanner scan = new Scanner(System.in);
   75   String input = scan.next();
   76   int inputLen=input.length();
   77   StringMethods sm=new StringMethods();
   78   String complmnt= sm.complement(input);
   79   //System.out.println("compl:"+complmnt); 
   80   String revrse=sm.reverse(complmnt);
   81   //System.out.println("rev:"+revrse);
   82   ArrayList<String> wrds=sm.subString(revrse,input);
   83   
   84   if (wrds.isEmpty()) {
   85    System.out.println("No reverse palindromic substrings available");
   86   }else{
   87    String[] outputs = wrds.toArray(new String[wrds.size()]);
   88    Arrays.sort(outputs);
   89    for(int i=0;i<outputs.length;i++){
   90     System.out.println(outputs[i]);
   91    } 
   92   }
   93  
   98   
   99   
  100  }
  101 }

No comments :

Post a Comment