π Day 12 β Teacherβs Day Challenge | Petya and Strings


ποΈ Date: August 05, 2025
π§© Platform: Codeforces
π’ Problems Solved Today: 1
π― Focus Topic: String Comparison, Lexicographic Order, Case Insensitivity
π Table of Contents
Daily Summary
π§© 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
Metric | Value |
Problems Solved | 1 |
Topics Covered | String Comparison |
Tools Used | C++ |
Next Focus | Sorting Strings, Custom Sorts |
Day | 12 / 30 |
π·οΈ Tags:#codeforces #strings #tolower #lexicographic #cpp #dsa #43DaysChallenge
Subscribe to my newsletter
Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
