Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Bob and Bombs problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes