Header Ad

HackerEarth Palindromes problem solution

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("%d\n",ans);
return 0;
}

Post a Comment

0 Comments