Codeforces 41A Translation Solution & Explanation

Difficulty : 800

Problem Description

The translation from the Berland language into the Birland language is not an easy task. Those languages are very similar: a berlandish word differs from a birlandish word with the same meaning a little: it is spelled (and pronounced) reversely. For example, a Berlandish word code corresponds to a Birlandish word edoc. However, it’s easy to make a mistake during the «translation». Vasya translated word s from Berlandish into Birlandish as t. Help him: find out if he translated the word correctly.

Input

The first line contains word s, the second line contains word t. The words consist of lowercase Latin letters. The input data do not consist unnecessary spaces. The words are not empty and their lengths do not exceed 100 symbols.

Output

If the word t is a word s, written reversely, print YES, otherwise print NO.

Eamples

Inputcode
edoc
OutputYES
Inputabb
aba
OutputNO
Inputcode
code
OutputNO

Understand this problem in 3 seconds:

Given two strings, check if one of them is the reverse of the other.


Solution

In this problem, let us find out some ways to reverse a string.

C# Solution

Solution1

string text1 = Console.ReadLine();
string text2 = Console.ReadLine();

char[]charArr1 = text1.ToCharArray();
char[]charArr2 = text2.ToCharArray();

Array.Reverse(charArr1);

Console.WriteLine(charArr1.SequenceEqual(charArr2) ? "YES" : "NO" );

*Note: When comparing two arrays in C#, using the == (equals) operator will only compare their references, not the actual content of the elements. So we need to use SequenceEqual here.

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text1 = scanner.nextLine();
        String text2 = scanner.nextLine();
        
        text1 = new StringBuilder(text1).reverse().toString();
        
        System.out.println(text1.equals(text2) ? "YES" : "NO");
    }
}

Conclusion

In this problem, we will learn how to reverse a string in various programming languages.

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 41A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *