Skip to content
Programmingoneonone
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
Programmingoneonone
Programmingoneonone

Learn everything about programming

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 solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes