Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone
Programmingoneonone

HackerEarth Exploring ruins problem solution

YASH PAL, 31 July 2024
In this HackerEarth Exploring ruins problem solution, Little robot CJ-17 is exploring ancient ruins. He found a piece of paper with a word written on it. Fortunately, people who used to live at this location several thousand years ago used only two letters of the modern English alphabet: ‘a’ and ‘b’. It’s also known, that no ancient word contains two letters ‘a’ in a row. CJ-17 has already recognized some of the word letters, the others are still unknown.
CJ-17 wants to look up all valid words that could be written on this paper in an ancient dictionary. He needs your help. Find him the word, which is the first in alphabetical order and could be written on the paper.
HackerEarth Exploring ruins problem solution

HackerEarth Exploring ruins problem solution.

#include <cstdio>

int s[1234567];

int main() {
int c = getchar();
while (c <= 32) c = getchar();
int n = 0;
while (c > 32) {
s[n++] = c;
c = getchar();
}
for (int i = 0; i < n; i++) {
if (s[i] != '?') continue;
if ((i > 0 && s[i - 1] == 'a') || (i + 1 < n && s[i + 1] == 'a')) {
s[i] = 'b';
} else {
s[i] = 'a';
}
}
for (int i = 0; i < n; i++) putchar(s[i]);
puts("");
}

Second solution

#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

#define y0 sdkfaslhagaklsldk

#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd
#define have adsgagshdshfhds
#define ends asdgahhfdsfshdshfd

#define eps 1e-8
#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512

#define ldouble long double

using namespace std;

long long INF = 1e9;
const int N = 210031;

string st;
string ans;

bool valid()
{
for (int i=0;i+1<st.size();i++)
{
if (st[i]=='a'&&st[i+1]=='a')
return false;
}
return true;
}

void backtrack(int ps)
{
if (!valid())
return ;

if (ans.size())
return;
if (ps==st.size())
{
if (valid())
{
ans=st;
}
return ;
}
if (st[ps]!='?')
{
backtrack(ps+1);
}
else
{
st[ps]='a';
backtrack(ps+1);
st[ps]='b';
backtrack(ps+1);
st[ps]='?';
}
}

int main(){
ios_base::sync_with_stdio(0);
//cin.tie(0);

cin>>st;

backtrack(0);

cout<<ans<<endl;

cin.get(); cin.get();
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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