A. Translation 41A code chef solution in java


    Introduction

    Codeforces 41A – Translation is a simple string manipulation problem that is especially useful for beginners learning Java strings, loops, character access, and string comparison.

    The problem is based on two fictional languages: Berlandish and Birlandish.

    The interesting rule is that a word in Berlandish becomes a word in Birlandish simply by writing its characters in reverse order.

    For example:

    Berlandish: code
    Birlandish: edoc
    

    So, if Vasya gives us two words, our task is to determine whether the second word is exactly the reverse of the first word.

    If it is, we print:

    YES
    

    Otherwise, we print:

    NO
    

    Codeforces 41A Translation – Problem Statement

    Vasya has a word s written in the Berlandish language. He translates it into another word t in the Birlandish language.

    According to the translation rule, the Birlandish version must be the reverse of the original Berlandish word.

    Given s and t, determine whether t is the correct reverse of s.

    Input

    The input contains two strings:

    • The first line contains the word s.

    • The second line contains the word t.

    Both strings contain only lowercase English letters.

    The strings are non-empty and their lengths do not exceed 100 characters.

    Output

    Print:

    YES
    

    if t is exactly the reverse of s.

    Otherwise, print:

    NO
    

    Example 1

    Input

    code
    edoc
    

    Reverse of code is:

    edoc
    

    The second word matches the reversed first word.

    Output

    YES
    

    Example 2

    Input

    abb
    aba
    

    Reverse of abb is:

    bba
    

    But the given second word is:

    aba
    

    They are different.

    Output

    NO
    

    Example 3

    Input

    code
    code
    

    The reverse of code is:

    edoc
    

    The second word is still:

    code
    

    Therefore, the translation is incorrect.

    Output

    NO
    

    Understanding the Main Idea

    The entire problem can be reduced to one simple operation:

    Reverse the first string and compare it with the second string.

    Suppose:

    s = hello
    

    Read the characters from right to left:

    o
    l
    l
    e
    h
    

    The reversed string is:

    olleh
    

    Now compare:

    olleh
    

    with the given second word.

    If they are equal, the answer is YES.

    Otherwise, the answer is NO.


    Java Solution

    Here is a clean and beginner-friendly implementation:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
    
            String firstWord = sc.next();
            String secondWord = sc.next();
    
            String reversedWord = "";
    
            // Build the reverse of the first word
            for (int i = firstWord.length() - 1; i >= 0; i--) {
                reversedWord += firstWord.charAt(i);
            }
    
            // Compare the reversed word with the second word
            System.out.println(
                reversedWord.equals(secondWord) ? "YES" : "NO"
            );
    
            sc.close();
        }
    }
    

    Step-by-Step Explanation of the Java Code

    1. Import Scanner

    import java.util.Scanner;
    

    The Scanner class is used to read input.

    Since Codeforces provides the input through standard input, we create a scanner using:

    Scanner sc = new Scanner(System.in);
    

    2. Read the Two Words

    String firstWord = sc.next();
    String secondWord = sc.next();
    

    The first statement reads the original word.

    The second statement reads the translated word.

    For example, if the input is:

    code
    edoc
    

    then:

    firstWord  = "code"
    secondWord = "edoc"
    

    The next() method is sufficient because the problem states that the words do not contain spaces.


    3. Create a Variable for the Reverse

    String reversedWord = "";
    

    Initially, reversedWord is an empty string.

    We will add characters to it one at a time, starting from the last character of firstWord.


    4. Traverse the String Backward

    The main part of the solution is:

    for (int i = firstWord.length() - 1; i >= 0; i--) {
        reversedWord += firstWord.charAt(i);
    }
    

    Normally, a string is processed from left to right.

    For example:

    c o d e
    0 1 2 3
    

    The indexes are:

    c → 0
    o → 1
    d → 2
    e → 3
    

    To reverse the string, we start at the last index:

    3
    

    and move toward:

    0
    

    So the characters are read in this order:

    e → d → o → c
    

    The resulting string becomes:

    edoc
    

    Why Do We Use length() - 1?

    Suppose the string is:

    code
    

    Its length is:

    4
    

    But Java indexes start at 0.

    Therefore, the valid indexes are:

    0, 1, 2, 3
    

    The last character is at:

    length() - 1
    

    which means:

    4 - 1 = 3
    

    So we start the loop with:

    int i = firstWord.length() - 1;
    

    Understanding charAt()

    The expression:

    firstWord.charAt(i)
    

    returns the character located at index i.

    For:

    code
    

    we have:

    firstWord.charAt(3) → 'e'
    firstWord.charAt(2) → 'd'
    firstWord.charAt(1) → 'o'
    firstWord.charAt(0) → 'c'
    

    Adding these characters produces:

    edoc
    

    5. Compare the Two Strings

    After reversing the first word, we have:

    reversedWord
    

    Now we need to check whether it matches:

    secondWord
    

    We use:

    reversedWord.equals(secondWord)
    

    This returns:

    true
    

    if both strings contain exactly the same characters in the same order.

    Otherwise, it returns:

    false
    

    Why Use equals() Instead of ==?

    This is an important concept for Java beginners.

    To compare the actual contents of two strings, use:

    equals()
    

    For example:

    reversedWord.equals(secondWord)
    

    Do not normally use:

    reversedWord == secondWord
    

    The == operator compares object references, while equals() compares the contents of the strings.

    For this problem, equals() is the correct choice.


    Understanding the Ternary Operator

    The final output uses:

    reversedWord.equals(secondWord) ? "YES" : "NO"
    

    This is called the ternary operator.

    It is a short alternative to an if-else statement.

    This:

    reversedWord.equals(secondWord) ? "YES" : "NO"
    

    means:

    If the strings are equal:
        print YES
    Otherwise:
        print NO
    

    The equivalent if-else code would be:

    if (reversedWord.equals(secondWord)) {
        System.out.println("YES");
    } else {
        System.out.println("NO");
    }
    

    Both approaches produce the same result.

    For beginners, the if-else version may initially be easier to read.


    Complete Dry Run

    Let's take:

    Input:
    code
    edoc
    

    Step 1: Read input

    firstWord = "code"
    secondWord = "edoc"
    

    Step 2: Start with an empty string

    reversedWord = ""
    

    Step 3: Start from the last character

    The length of code is 4.

    Therefore:

    Starting index = 4 - 1 = 3
    

    Character at index 3:

    e
    

    So:

    reversedWord = "e"
    

    Step 4: Move to index 2

    Character:

    d
    

    Now:

    reversedWord = "ed"
    

    Step 5: Move to index 1

    Character:

    o
    

    Now:

    reversedWord = "edo"
    

    Step 6: Move to index 0

    Character:

    c
    

    Now:

    reversedWord = "edoc"
    

    Step 7: Compare

    reversedWord = edoc
    secondWord   = edoc
    

    They are equal.

    Therefore:

    YES
    

    Another Dry Run

    Consider:

    Input:
    abb
    aba
    

    The first word is:

    abb
    

    Reverse it:

    bba
    

    Now compare:

    bba
    aba
    

    The strings are different.

    Therefore:

    NO
    

    A Simpler Way Using StringBuilder

    The above solution is perfectly understandable for a beginner, but Java provides StringBuilder, which is more appropriate when repeatedly adding characters to a string.

    An alternative solution is:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
    
            String firstWord = sc.next();
            String secondWord = sc.next();
    
            String reversedWord =
                    new StringBuilder(firstWord).reverse().toString();
    
            System.out.println(
                    reversedWord.equals(secondWord) ? "YES" : "NO"
            );
    
            sc.close();
        }
    }
    

    Here:

    new StringBuilder(firstWord)
    

    creates a mutable character sequence.

    Then:

    .reverse()
    

    reverses it.

    Finally:

    .toString()
    

    converts it back into a String.

    For someone learning loops and string manipulation, the first solution is useful because it shows how string reversal works internally.


    Time Complexity

    Let n be the length of the first word.

    We visit every character once to construct the reversed string.

    Therefore:

    Time Complexity: O(n)
    

    The comparison with the second string also takes up to O(n) time.

    So the overall complexity remains:

    O(n)
    

    The reversed string requires additional memory:

    Space Complexity: O(n)
    

    Given that the maximum word length is only 100 characters, this is easily within the problem's limits.


    Common Beginner Mistakes

    Mistake 1: Starting from index length()

    Incorrect:

    for (int i = firstWord.length(); i >= 0; i--)
    

    The last valid index is:

    firstWord.length() - 1
    

    So the correct version is:

    for (int i = firstWord.length() - 1; i >= 0; i--)
    

    Mistake 2: Comparing strings using ==

    Avoid:

    if (reversedWord == secondWord)
    

    Use:

    if (reversedWord.equals(secondWord))
    

    because we want to compare the actual string contents.


    Mistake 3: Reversing the wrong string

    The task says that t must be the reverse of s.

    Therefore, reverse:

    s
    

    and compare it with:

    t
    

    The logic is:

    reverse(s) == t
    

    Algorithm in Simple Steps

    The solution can be summarized as follows:

    1. Read the first word s.

    2. Read the second word t.

    3. Start from the last character of s.

    4. Build a new string by reading s backward.

    5. Compare the reversed string with t.

    6. Print YES if they match.

    7. Otherwise, print NO.

    Pseudocode

    Read s
    Read t
    
    reverse s
    
    If reversed s equals t:
        print YES
    Else:
        print NO
    

    Key Concepts Learned

    This beginner-level Codeforces problem teaches several useful Java concepts:

    • Reading strings using Scanner
    • Finding string length with length()
    • Accessing characters with charAt()
    • Traversing a string backward
    • Creating a reversed string
    • Comparing strings using equals()
    • Using the ternary operator
    • Understanding time and space complexity

    These concepts appear frequently in competitive programming and coding interviews.


    Final Java Code

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
    
            String firstWord = sc.next();
            String secondWord = sc.next();
    
            String reversedWord = "";
    
            for (int i = firstWord.length() - 1; i >= 0; i--) {
                reversedWord += firstWord.charAt(i);
            }
    
            System.out.println(
                reversedWord.equals(secondWord) ? "YES" : "NO"
            );
    
            sc.close();
        }
    }
    

    Conclusion

    Codeforces 41A – Translation is a simple but valuable string problem for Java beginners. The core idea is to reverse the first word and check whether the resulting string is exactly equal to the second word.

    The most important logic is:

    reverse(s) == t
    

    If this condition is true, the translation is correct and we print YES. Otherwise, we print NO.

    Once you understand backward string traversal, charAt(), and equals(), this problem becomes straightforward and provides a strong foundation for solving more advanced string-manipulation problems.

    SEO Keywords

    Codeforces 41A Java solution, Codeforces Translation solution, 41A Translation Java, Codeforces 41A solution in Java, Translation Codeforces problem, Java string reverse, Java reverse string program, Codeforces beginner problems, Java competitive programming, string manipulation in Java.