Before the prayer assembly begins, the students of different classes stand randomly in a single line. Often they become noisy. To maintain silence during the prayer assembly,the teacher tells them to stand in a particular
sequence. The sequence is such that there are at least N students of other classes standing between students of the same class.
La ca N A WNA
Write an algorithm to output the final
sequence in which the students will stand. If no
such sequence is possible then output -1. If
more than one sequence is possible, then
output the lexicographically smallest string.
inputs: ddgdhffnj
2
output 2
constraints:1<=count<=100
write a coding in java
Answers
When the prayer begins the students of different classes stand randomly in a single line
Answer:
Given a string str of N characters, the task is to find the lexicographically smallest string that can be formed by concatenating any prefix and its mirrored form.
Input: str = “bitsandbytes”
Output: bittib
Explanation: The lexicographically smallest string can be formed with the prefix “bit” as “bit” + “tib”.
Input: str = “abcd”
Output: aa
Java program to find lexicograohically smallest string
class ABC {
// function to sort the
// array of string
static void sort(String a[], int n)
{
//sort the array
for(int i = 0;i < n;i++)
{
for(int j = i + 1;j < n;j++)
{
// comparing which of the
// two concatenation causes
// lexicographically smaller
// string
if((a[i] + a[j]).compareTo(a[j] + a[i]) > 0)
{
String s = a[i];
a[i] = a[j];
a[j] = s;
}
}
}
}
static String lexsmallest(String a[], int n)
{
// Sort strings
sort(a,n);
// Concatenating sorted strings
String answer = "";
for (int i = 0; i < n; i++)
answer += a[i];
return answer;
}
// Driver code
public static void main(String args[])
{
String a[] = {"c", "cb", "cba"};
int n = 3;
System.out.println("lexicographically smallest string = "
+ lexsmallest(a, n));
}
}
To know more : https://brainly.in/question/45332594
https://brainly.in/question/50708215
#SPJ2