using
System;
using
System.Collections.Generic;
using
System.Collections;
public
class
GFG
{
public
static
int
maxLen = 0;
public
static
bool
iscontinuous(String temp)
{
var
last_Pos =
new
Dictionary<
char
,
int
>();
for
(
int
i = 0; i < temp.Length; i++)
{
if
(last_Pos.ContainsKey(temp[i]))
{
if
(i - last_Pos[temp[i]] + 1 <= 1)
{
last_Pos[temp[i]] = last_Pos[temp[i]] + 1;
}
else
{
return
false
;
}
}
else
{
last_Pos[temp[i]] = (last_Pos.ContainsKey(temp[i]) ? last_Pos[temp[i]] : 0) + 1;
}
}
return
true
;
}
public
static
void
generateSubSequences(String str, String temp,
int
j,
int
k)
{
var
freq =
new
Dictionary<
char
,
int
>();
if
(j == str.Length)
{
if
(temp.Length > 0)
{
var
minfreq =
int
.MaxValue;
var
maxfreq =
int
.MinValue;
freq.Clear();
for
(
int
i = 0; i < temp.Length; i++)
{
freq[temp[i]] = (freq.ContainsKey(temp[i]) ? freq[temp[i]] : 0) + 1;
}
foreach
(
int
i
in
freq.Values)
{
minfreq = Math.Min(minfreq,i);
maxfreq = Math.Max(maxfreq,i);
}
if
(maxfreq - minfreq <= k && GFG.iscontinuous(temp))
{
GFG.maxLen = Math.Max(GFG.maxLen,(
int
)temp.Length);
}
}
return
;
}
GFG.generateSubSequences(str, temp, j + 1, k);
temp += str[j];
GFG.generateSubSequences(str, temp, j + 1, k);
}
public
static
void
Main(String[] args)
{
var
str =
"abba"
;
var
temp =
""
;
var
k = 1;
GFG.generateSubSequences(str, temp, 0, k);
Console.Write(GFG.maxLen);
}
}