Sitemap
Hasanain Alsabonchi

Hasanain Alsabonchi

Software Developer

Complement DNA strand using Java

This blog will explain my approach to solving the “complementing DNA strands” problem.

Happy Coding

Algorithm:- Create Map called complemen.- Initialize map with key:char and value as Complement
ex. {'A':'T'}
- Loop i=n to 0:- Get a char from string at i- complemnt this char- convert char to string and add to the end
 Constraints:
below is a hint
* Each character s[i]is in the set [A,T,C,G].
STDIN Function Parameters
----- -------------------
ACCGGGTTTT → string s = "ACCGGGTTTT"
G T C A G → string s = "C T G A C"
static String result="";static HashMap <Character,Character> complement= new HashMap<>();

Get Hasanain Alsabonchi’s stories in your inbox

Join Medium for free to get updates from this writer.

public static String dnaComplement(String s) { complement.put('A','T'); complement.put('T','A'); complement.put('C','G'); complement.put('G','c'); int start=0; int end= s.length()-1; while(end>= start){ // used "" to convert character to string result+= ""+complement.get(s.charAt(end))+"";
end--;
}return result;}

Resources:

--

--