Back to challenges
Easy 0 solved 0% acceptance

DNA to RNA Transcription

Build an RNA string by replacing every DNA character with its partner.

What you'll learn

  • Translate characters with a lookup table
  • Build a new string from mapped characters
  • Handle every character in a sequence

Problem

Build an RNA string by replacing every DNA character with its partner. Use this complete mapping: A → U, C → G, G → C, T → A. Preserve the original order. No biology knowledge is needed.

Implement dna_to_rna(dna).

Input

dna: The DNA string to transform.

Output

Return the transformed string. Empty input returns an empty string.

Constraints

0 ≤ DNA length ≤ 100,000. Characters are uppercase A, C, G, or T.

Examples

Example 1
Input:
dna = "ACGT"

Expected output:
expected = "UGCA"
Input:
std::string dna = "ACGT";

Expected output:
std::string expected = "UGCA";
Input:
string dna = "ACGT";

Expected output:
string expected = "UGCA";

Explanation

A becomes U, C becomes G, G becomes C, and T becomes A, giving UGCA.

Example 2
Input:
dna = ""

Expected output:
expected = ""
Input:
std::string dna = "";

Expected output:
std::string expected = "";
Input:
string dna = "";

Expected output:
string expected = "";

Explanation

There are no characters to replace, so the result is empty.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Each DNA letter maps to exactly one RNA letter, so translate character by character.

Hint 2

Use a direct lookup for the four valid symbols while preserving the original order.

Hint 3

An empty input should return an empty string, and every character is guaranteed to be valid.

Expected complexity

O(n) time, O(n) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.