'Return lexicographically maximum possible proper string
Obtain a proper string that is lexicographically maximum. In one operation, you can swap a character It is given that a string is called a proper string if it has equal counts of 'a','b','c','d'. You can swap a character of the string with any of 'a','b','c','d'.
import java.util.*;
class Main
{
static int MAX = 26;
static String smallestStr(char []str, int n)
{
int i, j = 0;
int []chk = new int[MAX];
for (i = 0; i < MAX; i++)
chk[i] = -1;
for (i = 0; i < n; i++)
{
if (chk[str[i] - 'a'] == -1)
chk[str[i] - 'a'] = i;
}
for (i = 0; i < n; i++)
{
boolean flag = false;
for (j = 0; j < str[i] - 'a'; j++)
{
if (chk[j] > chk[str[i] - 'a'])
{
flag = true;
break;
}
}
if (flag)
break;
}
if (i < n)
{
char ch1 = str[i];
char ch2 = (char) (j + 'a');
for (i = 0; i < n; i++)
{
if (str[i] == ch1)
str[i] = ch2;
else if (str[i] == ch2)
str[i] = ch1;
}
}
return String.valueOf(str);
}
public static void main(String[] args)
{
String str = "aacacbbc";
int n = str.length();
System.out.println(smallestStr(
str.toCharArray(), n));
}
}
The output is supposed to be dadacbbc but my output is aababccb
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
