Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solved medium day 8 #92

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion medium/day_8/solution.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
//Write your code here
//Write your code here
#include <bits/stdc++.h>
using namespace std;
int getlength(string str, int idx1, int idx2)//this function returns the length of repeating sequence of string
{
int count = 0;//count shows how many continuous characters are repeating
while (idx1 < idx2 && idx2 < str.length())
{
if (str[idx1] == str[idx2])
{
idx1++;
idx2++;
count++;
}
else
break;
}
return count;
}
int main()
{
string str;
cin >> str; // input the string
int i = 0;
int maxLen = INT_MIN, len = 1;
bool flag = false; //flag shows if any character is repeating
while (i < str.length())//iterating over the string
{
if (str.find(str[i], i + 1) != string::npos) // if string find any repeating characters
{
flag = true; //character repeats so put flag to true
len = getlength(str, i, str.find(str[i], i + 1));//now get the length of repeating sequence
if (len > maxLen)
{
maxLen = len;//if length of current repeating sequence is greater than len of
//maximum repeating sequence till now, then update maxlen
}
}
i++;
}
if (flag)//if flag is true means there is atleast one repeating sequence
cout << maxLen << endl;
else
cout << -1 << endl;
return 0;
}