Skip to content

Commit

Permalink
updated solution to day 8 medium
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayushivam22 committed Apr 5, 2024
1 parent e536494 commit 7b01bde
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions medium/day_8/solution.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//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]
Expand All @@ -23,31 +23,37 @@ int main()
string str;
string AnsStr;
cin >> str; // input the string
if(str.length() == 1)
if (str.length() == 1)
{
cout<<-1<<endl;
cout << -1 << endl;
exit(0);
}
int i=0;
int MaxLength=INT_MIN;
while(i<str.length())//iterate over the string
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
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;
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;
if (AnsStr.length() == 0)
{
cout << -1 << endl;

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

0 comments on commit 7b01bde

Please sign in to comment.