Complement DNA strand using Java
Mar 28, 2022
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<>();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))+"";Get Hasanain Alsabonchi’s stories in your inbox
Join Medium for free to get updates from this writer.
end--; }return result;}
Resources:








