πŸ“˜ Day 12 – Teacher’s Day Challenge | Petya and Strings

TanishiTanishi
2 min read

πŸ—“οΈ Date: August 05, 2025
🧩 Platform: Codeforces
πŸ”’ Problems Solved Today: 1
🎯 Focus Topic: String Comparison, Lexicographic Order, Case Insensitivity


πŸ”— Table of Contents


🧩 Problem 1 – Petya and Strings

πŸ“š Difficulty: Easy
🧠 Concepts: String Transformation, Case-Insensitive Comparison, Lexicographical Order


πŸ“„ Problem Statement (Summary):
Petya receives two strings of equal length containing uppercase and lowercase letters. He wants to compare them lexicographically, ignoring case sensitivity.

Your task is to:

  • Convert both strings to lowercase.

  • Compare them:

    • Output -1 if the first string is lexicographically smaller.

    • Output 1 if the first is greater.

    • Output 0 if they are equal.

Examples:
Input:
aaaa
aaaA
Output: 0

Input:
abs
Abz
Output: -1

Input:
abcdefg
AbCdEfF
Output: 1


πŸ’‘ Approach:

  • Read both strings.

  • Use transform() to convert both to lowercase.

  • Compare character by character:

    • If any character in str1 < str2 β†’ print -1

    • If any character in str1 > str2 β†’ print 1

    • If none differ β†’ print 0


πŸ§ͺ Code (C++):

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    string str1, str2;
    cin >> str1 >> str2;
    transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
    transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
    for (int i = 0; i < str1.length(); i++)
    {
        if (str1[i] < str2[i])
        {
            cout << -1;
            return 0;
        }
        else if (str1[i] > str2[i])
        {
            cout << 1;
            return 0;
        }
    }
    cout << 0;
    return 0;
}

πŸ“Έ Submission Screenshot:


βœ… Key Takeaways:

  • transform() with ::tolower is perfect for case-insensitive tasks.

  • Lexicographical comparison is simply done with direct character comparison.

  • Always check equality only after confirming no differences were found.


πŸ“ˆ Daily Summary

MetricValue
Problems Solved1
Topics CoveredString Comparison
Tools UsedC++
Next FocusSorting Strings, Custom Sorts
Day12 / 30

🏷️ Tags:
#codeforces #strings #tolower #lexicographic #cpp #dsa #43DaysChallenge

0
Subscribe to my newsletter

Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tanishi
Tanishi