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 Rotation problem solution

YASH PAL, 31 July 2024
In this HackerEarth Rotation problem solution You are given two strings S and T of the same length N. Your task is to convert the string S into T by doing some operations. In an operation, you can delete the first character of the string S and append any character at the end of the string. You are required to determine the minimum number of operations to convert S into T.
HackerEarth Rotation problem solution

HackerEarth Rotation problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007ll
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define x first
#define y second
ll lps[1000005], match[1000005], n;
string s, t;
void com_lps()
{
ll j = 0, i = 1;
lps[0] = 0;
while(i < n)
{
if(s[i] == s[j])
{
j ++;
lps[i] = j;
i ++;
}
else
{
if(j)
j = lps[j - 1];
else
{
lps[i] = 0;
i ++;
}
}
}
}
ll solve()
{
memset(match, 0, sizeof(match));
ll i = 0, j = 0;
while(i < n)
{
if(t[j] == s[i])
{
j ++;
match[i] = j;
i ++;
if(j == n)
return 0;
}
else
{
if(j)
j = lps[j - 1];
else
i ++;
}
}
return n - match[n - 1];
}
int main()
{
// freopen("inp.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> n;
cin >> s;
cin >> t;
com_lps();
cout << solve();
// fclose(stdin);
// fclose(stdout);
return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e7 + 17;
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
string s, p;
cin >> s >> p;
reverse(s.begin(), s.end());
for(int i = 0; ; i++){
if(string(s.rbegin(), s.rend()) == p)
return cout << i << 'n', 0;
s.pop_back();
p.pop_back();
}
}
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