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);
  }
}