Header Ad

HackerEarth One String No Trouble problem solution

In this HackerEarth One String, No Trouble problem solution A string S is called a good string if and only if two consecutive letters are not the same. For example, abcab and cda are good while abaa and accba are not. You are given a string S. Among all the good substrings of S, print the size of the longest one.


HackerEarth One String No Trouble problem solution


HackerEarth One String No Trouble problem solution.

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back

const int maxn = 2e5 + 20;

int a[maxn];

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);

string s;
cin >> s;

char last = 'a' - 1;
int len = 0 , res = 0;
for(auto ch : s)
{
if(ch == last)
len = 0;
len++;
last = ch;
res = max(res , len);
}

cout << res << endl;
}


second solution

#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

template<typename T> T mo(T x, T y) { x %= y; return x <= 0 ? x + y : x; }

int main() {
#ifdef FASTIO
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
string s;
cin >> s;
int n = s.size();
int len = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
len++;
if (i < n && s[i] == s[i + 1]) {
ans = max(ans, len);
len = 0;
}
}
ans = max(ans, len);
cout << ans << "\n";
return 0;
}


Post a Comment

0 Comments