๐ Day 17 โ Teacher's Day Challenge | Stones on the Table


๐๏ธ Date: August 10, 2025
๐งฉ Platform: Codeforces
๐ข Problems Solved Today: 1
๐ฏ Focus Topic: Strings, Consecutive Characters
๐ Table of Contents
Daily Summary
๐งฉ Problem 1 โ Stones on the Table
๐ Difficulty: Easy-Medium
๐ง Concepts: Strings, Counting Consecutive Elements
๐ Problem Statement (Summary):
You are given n
stones in a row, each stone being Red (R), Green (G), or Blue (B).
You must find the minimum number of stones to remove so that no two neighboring stones have the same color.
๐ข Input Format:
First line: integer
n
(1 โค n โค 50) โ number of stones.Second line: string
s
of lengthn
, wheres[i]
โ {R, G, B}.
๐งพ Output Format:
A single integer โ minimum stones to remove.
๐ Example 1:
Input:
3
RRG
Output:
1
๐ Example 2:
Input:
5
RRRRR
Output:
4
๐ Example 3:
Input:
4
BRBG
Output:
0
๐ก Approach:
Traverse the string from left to right.
Count every instance where
s[i] == s[i+1]
(two consecutive same colors).This count is the minimum stones to remove.
๐งช Code (C++):
#include <iostream>
using namespace std;
int main() {
int n, c1=0;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) {
c1++;
}
}
cout << c1 << endl;
return 0;
}
๐ธ Submission Screenshot:
โ Key Takeaways:
Simple linear scan solves the problem efficiently.
Always remember
n-1
in loop to avoid out-of-bounds errors.Problems with small constraints often just require careful counting logic.
๐ Daily Summary
Metric | Value |
Problems Solved | 1 |
Topics Covered | Strings, Consecutive Characters |
Tools Used | C++ |
Next Focus | More String Problems & Pattern Recognition |
Day | 17 / 43 |
๐ท๏ธ Tags:#codeforces #strings #cpp #beginner #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
