![Strings Beginner level DSA problem solution codechef](/media/blog_images/codechef.png)
Problem Title: DNA Storage
Statement:
For encoding an even-length binary string into a sequence of A
, T
, C
, and G
, we iterate from left to right and replace the characters as follows:
00
is replaced withA
01
is replaced withT
10
is replaced withC
11
is replaced withG
Given a binary string S of length N (N is even), find the encoded sequence.
Input Format:
- The first line will contain T, the number of test cases. Then the test cases follow.
- Each test case contains two lines of input.
- The first line contains a single integer N, the length of the sequence.
- The second line contains binary string S of length N.
Output Format:
For each test case, output in a single line the encoded sequence.
Note: Output is case-sensitive.
Constraints
- 1≤T≤100
- 2≤N≤10^3
- N is even.
- The sum of N over all test cases is at most 10^3.
- S contains only characters
0
and1
.
Sample 1:
Input
4
2
00
4
0011
6
101010
4
1001
Output
A
AG
CCC
CT
Explanation:
Test case 1: Based on the rules 00
is replaced with A
.
Test case 2: Based on the rules 00
is replaced with A
. Similarly, 11
is replaced with G
. Thus, the encoded sequence is AG
.
Test case 3: The first two characters are 10
which is encoded as C
. Similarly, the next two characters 10
are encoded as C
, and the last two characters 10
are encoded as C
. Thus, the encoded string is CCC
.
Test case 4: The first two characters are 10
which is encoded as C
. Similarly, the next two characters 01
are encoded as T
. Thus, the encoded string is CT
.
Solution in Python3
# Input parsing
T = int(input().strip())
# Process each test case
for _ in range(T):
N = int(input().strip())
S = input().strip()
# Encode the binary string
encoded_sequence = ""
for i in range(0, N, 2):
pair = S[i:i+2]
if pair == "00":
encoded_sequence += "A"
elif pair == "01":
encoded_sequence += "T"
elif pair == "10":
encoded_sequence += "C"
elif pair == "11":
encoded_sequence += "G"
# Print the encoded sequence for this test case
print(encoded_sequence)
Solution explanation & approach
-
Input Reading: We will first read how many sets of binary strings we need to process (T). Then, for each set, we read the length of the binary string (N) and the string itself (S).
-
Encoding: For each set, we go through the binary string two characters at a time. For every pair:
- If the pair is "00", we replace it with "A".
- If the pair is "01", we replace it with "T".
- If the pair is "10", we replace it with "C".
- If the pair is "11", we replace it with "G".
-
Output: After processing each pair in the binary string, we print the resulting encoded sequence.
This approach ensures that we encode each pair according to the given rules and print the encoded sequence for each set of binary strings.
Conclusion
this was the solution of Beginner level DSA problem DNA Storage solution and the approach how to solve the strings problems.
I hope you all like this blog. keep learning, keep exploring.????
Do you dream of turning your thoughts and words into income ? join us now and become blogger now.
0 Comments
Leave a Comment