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
  • 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 String Operations problem solution

YASH PAL, 31 July 2024
In this HackerEarth String Operations problem solution, You are given a string S.
Q number of operations are performed on string S.
In each of these Q operations, you are given an index ind and a character ch. For each operation, you have to update the character at index ind in string S to ch, that is, after this operation S[ind] = ch.
It is guaranteed that any index is updated at most once.
We call the final string after performing the Q number of operations as str.
After this, M number of operations are performed on string str.
In each of these M operations, you are given two indices a and b. For each operation, you have to reverse the substring that lies between the indices a and b (inclusive).
We call the final string after performing M operations as fin.
HackerEarth String Operations problem solution

HackerEarth String Operations problem solution.

import java.util.*;
import java.lang.*;

class StringQue
{
public static void main(String args[])
{
StringBuffer s= new StringBuffer();
Scanner in= new Scanner(System.in);
s.append(in.nextLine());
int q=in.nextInt();
for(int i=1;i<=q;i++)
{
int ind=in.nextInt();
char ch=in.next().charAt(0);
s.setCharAt(ind-1,ch);
}
StringBuffer str=new StringBuffer(s);
int m=in.nextInt();
for(int i=1;i<=m;i++)
{
int a=in.nextInt(),b=in.nextInt(),ans=0;
StringBuffer upd=new StringBuffer();
upd.append(s.substring(a-1,b));
upd.reverse();
s.replace(a-1,b,upd.toString());
}
int ans=0;
System.out.println(str);
System.out.println(s);
for(int i=0;i<s.length();i++)
{
if(str.charAt(i)==s.charAt(i))
ans++;
}
System.out.println(ans);
}
}
coding problems

Post navigation

Previous post
Next post
  • 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
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes