Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerEarth One String No Trouble problem solution

YASH PAL, 31 July 2024
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;
}
coding problems

Post navigation

Previous post
Next post
  • 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
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programmingoneonone | WordPress Theme by SuperbThemes