Skip to content

Commit

Permalink
updated solution day 8 medium
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayushivam22 committed Apr 5, 2024
1 parent 53a3349 commit e536494
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion medium/day_8/solution.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
//Write your code here
//Write your code here
#include <bits/stdc++.h>
using namespace std;
string getsequence(string str, int idx1, int idx2) // this function returns the repeating sequence of string starting with str[i]
{

string temp = ""; // temp shows the repeating sequence
while (idx2 < str.length())
{
if (str[idx1] == str[idx2])
{
temp += str[idx1];
idx1++;
idx2++;
}
else
break;
}
return temp;
}
int main()
{
string str;
string AnsStr;
cin >> str; // input the string
if(str.length() == 1)
{
cout<<-1<<endl;
exit(0);
}
int i=0;
int MaxLength=INT_MIN;
while(i<str.length())//iterate over the string
{
int j=i;
while(j<str.length())//j represents all the indices where str[i] occurs except the first occurence
{
j=str.find(str[i],j+1);
if(j==-1) break; // if there is no more occurence of str[i] is left then we break the loop
string temp = getsequence(str,i,j);//get the sequence of string which is repeating
int len=temp.length();
if(len > MaxLength) //if length of current repeating sequence is greater than previously stored length
{ // then update max length
AnsStr=temp;//store max repeating sequence in AnsStr
MaxLength=len;
}
}
i++;
}

cout<<AnsStr<<endl;
return 0;
}
Binary file added medium/day_8/solution.exe
Binary file not shown.

0 comments on commit e536494

Please sign in to comment.