You are on page 1of 1

class AB

{
public String createString(int N, int K)
{
int limit = (N*N)/4;
if(K > limit)
return "";
char str[] = new char[N];
for(int i=0; i<N; i++)
str[i] = 'A';
if(N > 0)
{
int pos = N-1;
int count = 0;
int countA = N;
int countB = 0;
while(pos >= 0 && count != K)
{
str[pos] = 'B';
countA --;
countB ++;
count = countA * countB;
if(count == K)
break;
else if(count < K)
pos --;
else
{
str[pos] = 'A';
pos = pos - (count - K);
}
}
}
return String.valueOf(str);
}
}

You might also like