Codeforces 112A Petya and Strings Solution & Explanation

Difficulty : 800

Problem Description

Little Petya loves presents. His mum bought him two strings of the same size for his birthday. The strings consist of uppercase and lowercase Latin letters. Now Petya wants to compare those two strings lexicographically. The letters’ case does not matter, that is an uppercase letter is considered equivalent to the corresponding lowercase letter. Help Petya perform the comparison.

Input

Each of the first two lines contains a bought string. The strings’ lengths range from 1 to 100 inclusive. It is guaranteed that the strings are of the same length and also consist of uppercase and lowercase Latin letters.

Output

If the first string is less than the second one, print “-1”. If the second string is less than the first one, print “1”. If the strings are equal, print “0”. Note that the letters’ case is not taken into consideration when the strings are compared.

Examples

Inputaaaa
aaaA
Output0
Inputabs
Abz
Output-1
Inputabcdefg
AbCdEfF
Output1

Note

If you want more formal information about the lexicographical order (also known as the “dictionary order” or “alphabetical order”), you can visit the following site:

Understand this problem in 3 seconds:

Compare two input strings while ignoring their case.

If the first string is less than the second, print -1

If they are same, print 0

If the first string is greater than the second, print 1


Solution

There have some built-in functions that we can use to compare two strings.

C# Solution

Solution1string.compare

string a = Console.ReadLine();
string b = Console.ReadLine();

int result = string.Compare(a, b, StringComparison.OrdinalIgnoreCase);

if (result > 0)
{
    Console.WriteLine(1);
}
else if (result == 0)
{
    Console.WriteLine(0);
}
else
{
    Console.WriteLine(-1);
}

We can use a built-in function string.compare to compare two strings

And with it’s parameters StringComparison.OrdinalIgnoreCase, it can ignore case easily.

Additionally, you can achieve case-insensitive comparison by using methods like .ToLower() or .ToUpper().

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
    
        String a = scanner.nextLine();
        String b = scanner.nextLine();

        int result = a.compareToIgnoreCase(b);

        if (result > 0) {
            System.out.println(1);
        } else if (result == 0) {
            System.out.println(0);
        } else {
            System.out.println(-1);
        }
    }
}

Python3 Solution

Solution1

a = input()
b = input()

a = a.lower()
b = b.lower()

if(a<b):
    print(-1)
elif(a==b):
    print(0)
else:
    print(1)

In Python, you can easily compare strings using operators.

For example, you can use the ‘==’ operator to check if two strings are equal, and you can use ‘<‘ and ‘>’ operators to determine the relative order of strings in a case-sensitive manner.


Conclusion

This problem is testing your familiarity with how to compare strings, and being familiar with built-in functions can be quite helpful.

Reference

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts or clicking ads, 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 – 112A – Codeforces

Random Codeforces Posts

Leave a Reply

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