I know the O(N*26*26) solution , while traversing the string , at each index i , iterate from 1 to 26 letters, and check the most recent occurrence of a character whose both forms(uppercase and lowercase) haven't appeared yet. Note down its index 'j' . s[j+1....i] is to be noted. Character with maximum 'j' value will create a string[j+1...i] , remember its length.Now, some characters in this string will still be faulty , they can be removed by repeating the process 26 more times .
Do that for each character at each index and you are done .
Code :
#include<bits/stdc++.h>
typedef long long int ll ;
class Solution {
public:
string longestNiceSubstring(string s)
{
int a[30]={0}; //for smaller_case_guys!!
int b[30]={0};
int c[30]={0};
int d[30]={0};
int n = s.size() ;
int god[200][3];
ll k = 0 ;
int i = 0 ;
while(i<n)
{
int g = int(s[i]) ;
int y ;
if(g>=97)
{
y = g-96 ;
a[y]=1;
c[y]=i;
}
else
{
y = g-64 ;
b[y]=1;d[y]=i ;
}
int need = -605 ;
int j = 1 ;
while(j<=26)
{
if(a[j]==0 && b[j]==1)
{
need = max(need,d[j]) ;
}
else if(b[j]==0 && a[j]==1)
{
need = max(need,c[j]) ;
}
else
{
}
j++;
}
if(need==-605)
{
need = -1 ;
}
int l = 1 ;
while(l<=26)
{
j=1 ;
while(j<=26)
{
if(a[j]==1 && b[j]==1)
{
//need = max(need,d[j]) ;
if(c[j]>need && d[j]>need)
{
//RAM_RAM!!!!!!!
}
else
{
//RAM_RAM!!!
need = max(need,c[j]) ;
need = max(need,d[j]) ;
}
}
j++;
}
l++;
}
if(need!=-605 && need<i)
{
god[k][0]=need+1;
god[k][1]=i;
k++;
}
i++;
}
int yep = 0 ;
i=0;
while(i<k)
{
int diff = abs(god[i][0]-god[i][1]) + 1 ;
yep = max(diff,yep);
i++;
}
string pass="";
if(yep==0)
{
return pass ;
}
else
{
int rem = n+8 ;
i=0;
while(i<k)
{
int diff = abs(god[i][0]-god[i][1]) + 1 ;
if(diff==yep)
{
rem = min(rem,god[i][0]);
}
i++;
}
string finally = s.substr(rem,yep);
return finally ;
}
}
};
Problem-1 : https://leetcode.com/contest/biweekly-contest-46/problems/longest-nice-substring/
Explanation : Generate all possible substrings of the strings in O(n^2) by using 2 loops .
For each generated string , check if its a good string or not in O(n) using check function . If the substring is good, note down its length and starting position . Output the substring with largest length of all such good substrings .
We use a check() function, to check if this substring contains all lowercase as well as uppercase letters . We use two arrays 'a' and 'b' of size 30 for that purpose .
Code :
#include<bits/stdc++.h>
typedef long long int ll ;
ll check(string x)
{
ll a[30]={0};
ll b[30]={0};
ll i = 0 ;
while(i<x.size())
{
ll g = int(x[i]);
if(g>=97)
{
ll u = g-96 ;
if(a[u]==0)
{
a[u]=1;
}
}
else
{
ll u = g-64 ;
if(b[u]==0)
{
b[u]=1;
}
}
i++;
}
ll good = 1 ;
i=1;
while(i<=26)
{
if(a[i]!=b[i])
{
good = 0 ;
}
i++;
}
return good ;
}
class Solution {
public:
string longestNiceSubstring(string s)
{
string win="";
ll maxi = 0 ;
ll n = s.size() ;
ll i=0;
while(i<n)
{
ll j ;
j=i ;
while(j<n)
{
ll length = abs(i-j) + 1 ;
string s1 = s.substr(i,length);
ll good = check(s1);
if(good==1)
{
if(length>maxi)
{
maxi = length ;
win = s1 ;
}
}
j++;
}
i++;
}
return win ;
}
};
We use s.substr(x,y) function in C++ to make implementation easy !
O(n^2) solution is also possible . I leave that upto the reader .