Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Bob and Bombs problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Bob and Bombs problem solution, Bob and Khatu are brave soldiers in World War 3. They have spotted an enemy troop that is planting bombs. They sent messages to the command center containing characters W and B where W represents a wall and B represents a Bomb.
 
They asked the command to tell them how many walls will be destroyed if all bombs explode at once. One bomb can destroy 2 walls on both sides.
 
 
HackerEarth Bob and Bombs problem solution

 

 

HackerEarth Bob and Bombs problem solution.

#include<bits/stdc++.h>

using namespace std;

#define vi vector < int >
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define foreach(it,v) for( __typeof((v).begin())it = (v).begin() ; it != (v).end() ; it++ )
#define ll long long
#define llu unsigned long long
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define dbg(x) { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define all(x) x.begin(),x.end()
#define mset(x,v) memset(x, v, sizeof(x))
#define sz(x) (int)x.size()

int main()
{
int t;
cin >> t;
assert(1 <= t && t <= 10);
while(t--)
{
string s;
cin >> s;
int n = sz(s) , i;
int ans = 0 , pre = -1;
assert(1 <= n && n <= 100000);
for(i=0;i<n;i++)
{
assert(s[i] == 'B' || s[i] == 'W');
}
for(i=0;i<n;i++)
{
if(s[i] == 'B')
{
if(pre == -1)
{
ans += min(2,i);
}
else
{
ans += min(4,i - pre - 1);
}
pre = i;
}
}
if(pre != -1)
ans += min(2,n - pre - 1);
printf("%dn",ans);
}
return 0;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

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

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes