๐Ÿ“˜ Day 17 โ€“ Teacher's Day Challenge | Stones on the Table

TanishiTanishi
2 min read

๐Ÿ—“๏ธ Date: August 10, 2025
๐Ÿงฉ Platform: Codeforces
๐Ÿ”ข Problems Solved Today: 1
๐ŸŽฏ Focus Topic: Strings, Consecutive Characters


๐Ÿ”— Table of Contents


๐Ÿงฉ 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 length n, where s[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

MetricValue
Problems Solved1
Topics CoveredStrings, Consecutive Characters
Tools UsedC++
Next FocusMore String Problems & Pattern Recognition
Day17 / 43

๐Ÿท๏ธ Tags:
#codeforces #strings #cpp #beginner #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