Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • 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
Programmingoneonone

Learn everything about programming

HackerEarth Palindromes problem solution

YASH PAL, 31 July 2024
In this HackerEarth Palindromes problem solution Everybody loves palindromes, but Artur doesn’t.
He has a string S that consists of lowercase English letters (‘a’ – ‘z’). Artur wants to find a substring T of S of the maximal length, so that T isn’t a palindrome.
HackerEarth Palindromes problem solution

HackerEarth Palindromes problem solution.

#include <bits/stdc++.h>

using namespace std;

int main()
{
string s;
cin >> s;
int n = s.size();
int cnt = 0;
for (int i = 0; i < n; i++)
cnt += s[i] == s[0];
if (cnt == n)
{
cout << "0";
return 0;
}
cnt = 0;
for (int i = 0; i < n; i++)
cnt += s[i] == s[n - i -1];
if (cnt == n)
{
cout << n - 1;
return 0;
}
cout << n;
return 0;
}

Second 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()

bool isPalin(string s)
{
int l = 0 , r = sz(s) - 1;
while(l < r && s[l] == s[r])
{
l++,r--;
}
return (l == r);
}

bool allSame(string s)
{
int i;
for(i=1;i<sz(s);i++)
{
if(s[i] != s[i-1])
return 0;
}
return 1;
}

int main()
{
string s;
cin>>s;
assert(1 <= sz(s) && sz(s) <= 100000);
int ans;
if(allSame(s))
ans = 0;
else if(isPalin(s))
ans = sz(s) - 1;
else
ans = sz(s);
printf("%dn",ans);
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

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
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes