Problem:-
A string is called a good string if and only if two consecutive letters are not the same. For example, and are good while and are not.
You are given a string . Among all the good substrings of ,print the size of the longest one.
Input format
A single line that contains a string ().
Output format
Print an integer that denotes the size of the longest good substring of .
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
The complete string is good so the answer is .
Code:-
Here I am going to give you two solution first one is on the basis of C language and second one is on the basis of c++ language which you can submit in c++14 and c++17 also
Solution 1 ( C language):-
#include<stdio.h>
#include<string.h>
int main()
{
char a[200001];
long int i,m=0,max,s[200000];
scanf ("%s",a);
for(i=0;a[i]!='\0';i++)
{
if (a[i]==a[i+1])
{
s[m]+=1;
m++;
continue;
}
else
{
s[m]+=1;
}
}
max=s[0];
for(i=1;s[i]!='\0';i++)
{
if(s[i]>max)
max=s[i];
}
printf ("%lld",max);
return 0;
}
Solution 2 ( C++ language):-
This solution is based on the c++ language and you can submit ib c++14 and c++17 also.
#include<iostream>
using namespace std;
int main()
{
string s;
int sum = 1;
cin >> s;
int max = 1;
for(int i = 1; i<s.length(); i++)
{
if(s[i] != s[i-1])
sum++;
else
{
if(sum > max)
max = sum;
sum = 1;
}
}
if(sum > max)
max = sum;
cout << max;
}
Recommended Post:-
- Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-Data structure:-- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
0 Comments