Case Specific Sorting Of String
Problem :
sort uppercase and lowercase letters such that if the ith place in the original string had an Uppercase character then it should not have a lowercase character after being sorted and vice versa
Explanation:
Take two arraylist for smaller and upper case letters and sort them .
Print according to original string alphabets.
Code:
import java.util.*;
import java.lang.*;import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
ArrayList<Character> lower=new ArrayList<Character>();
ArrayList<Character> upper=new ArrayList<Character>();
int n=ab.nextInt();
int i=0;
String data=ab.next();
while(i<n)
{
if(Character.isUpperCase(data.charAt(i)))
upper.add(data.charAt(i++));
else
lower.add(data.charAt(i++));
}
Collections.sort(upper);
Collections.sort(lower);
i=0;
while(i<n)
{
if(Character.isUpperCase(data.charAt(i++)))
System.out.print((char)upper.remove(0));
else
System.out.print((char)lower.remove(0));
}
System.out.print("\n");
}
}
}
0 Comments:
Post a Comment