DNASTRAND - Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4

Setter: daanish_adm, Utkarsh_25dec
Testers: inov_360, iceknight1093
Editorialist: hrishik85

DIFFICULTY:

660

PREREQUISITES:

None

PROBLEM:

We are given a string S of length N. The only characters in the string are ‘A’, ‘T’, ‘C’ and ‘G’. We know that A is complementary to T and T is complementary to A. Similarly, C is complementary to G and G is complementary to C.
We have to output the string that is complementary to S.

EXPLANATION:

This is an implementation problem.
There are 2 lines per test case, your input acceptance needs to take this into consideration.
We start with defining an empty string S’ that is complementary to S.
We need to iterate through string S from left to right once
For each element of the string S, we check the character and append its complementary character to S’. Once we have traversed the entire string S, our complementary string S’ will be completely populated and we can output the same

TIME COMPLEXITY:

Time complexity is O(N).

SOLUTION:

Tester's Solution
for _ in range(int(input())):
	n = int(input())
	s = input()
	mapping = {ord('A') : ord('T'), ord('T') : ord('A'), ord('G') : ord('C'), ord('C') : ord('G')}
	s.translate(mapping)
	print(s.translate(mapping))
Editorialist's Solution
t=int(input())
for _ in range(t):
    n=int(input())
    S=str(input())
    Scomp=str()
    i=0
    while i<n:
        if S[i]=='A':
            Scomp = Scomp + 'T'
        elif S[i]=='T':
            Scomp = Scomp + 'A'
        elif S[i]=='C':
            Scomp = Scomp + 'G'
        elif S[i]=='G':
            Scomp = Scomp + 'C'
        i=i+1
    print(Scomp)