From bce92195a70ae5cbecc360309dd446415dbd417d Mon Sep 17 00:00:00 2001 From: Ayush Kr Shivam <106876019+Ayushivam22@users.noreply.github.com> Date: Fri, 5 Apr 2024 10:52:35 +0530 Subject: [PATCH] solved medium day 8 --- medium/day_8/solution.cpp | 47 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/medium/day_8/solution.cpp b/medium/day_8/solution.cpp index 4f32122..339c333 100644 --- a/medium/day_8/solution.cpp +++ b/medium/day_8/solution.cpp @@ -1 +1,46 @@ -//Write your code here \ No newline at end of file +//Write your code here +#include +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; +}