Given a string that contains uppercase letters as well as lower case letters. The task is to move all uppercase characters at the end of the String. The uppercase characters must be in the same order as in the original string.
Input : "heLLGFg" Output : "hegLLGF" Input : "Hello" Output : "elloH"
Here we are having two different approaches to get to the problem namely as follows:
- Using ASCII values of the characters.
- Using queue data structures
Approach 1: Using ASCII values of the characters.
- Iterate the character of the string one by one and checks the ASCII values of the respective character of the string.
- For all uppercase letters, ASCII values lie under [65-90] char values. Store the characters of these ASCII values in the String variable and print the string at the end of the Iteration.
Example:
// Java Program to Move All Uppercase Characters to the End // Importing input output classes import java.io.*;
// Mai class class GFG {
// Method 1
// To shift uppercase characters
static void shiftuppercase(String m, int length)
{
// Taking an empty string
String temp = "" ;
for ( int i = 0 ; i < length; ++i) {
// Condition check
// If the character is uppercase via
// the ASCII values of the character
if (m.charAt(i) >= 65 && m.charAt(i) <= 90 ) {
temp += m.charAt(i);
}
// The character is already lowercase
else
System.out.print(m.charAt(i));
}
// Now, Printing the uppercase string
System.out.print(temp);
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Custom input string
String m = "heLLGFg" ;
// Computing the length of the string
// using length() method
int length = m.length();
// Calling the method 1 over the custom string taken
// above to move all uppercase char to the end
shiftuppercase(m, length);
}
} |
Output:
Time Complexity: O(n) //n is the length of the string.
Space Complexity: O(1)
Approach 2: Using queue data structures
- Queue Data structure implements the First in First out concept, We use this concept and store the uppercase characters in the queue.
- Print all the characters except the uppercase characters.
- After the iteration removes all the elements from the queue.
Example:
// Java Program to Move All Uppercase Characters to the End // Using Queue data structures // Importing input output classes import java.io.*;
// Importing utility classes import java.util.*;
// Main class class GFG {
// Method 1
// Main driver method
static void shiftuppercase(String m, int length)
{
// Creating an object of Queue class of character
// type
Queue<Character> Q = new LinkedList<Character>();
// Declaring an initializing to empty string
String temp = "" ;
for ( int i = 0 ; i < length; ++i) {
// Condition checkfor the uppercase characters
// If uppercase use ASCII values of the
// character
if (m.charAt(i) >= 65 && m.charAt(i) <= 90 ) {
Q.add(m.charAt(i));
}
// Character is lowercase
else
// Leave it ascites on its index
System.out.print(m.charAt(i));
}
// Now, printing the uppercase string till
// there are elements in queue
while (Q.size() != 0 ) {
// Removing all the elements from the queue
System.out.print(Q.peek());
// Clear the queue
Q.remove();
}
}
// Method 2
// main driver method
public static void main(String[] args)
{
// Given input string
String m = "heLLGFg" ;
// Computing the length of the string
// using length() method
int length = m.length();
// Calling the
shiftuppercase(m, length);
}
} |
Output:
Time Complexity: O(N) // where N is the length of the given string.
Auxiliary Space: O(n)
Approach 3 : Using isUpperCase() method
// Java Program to Move All Uppercase Characters // to the End // Importing input output classes import java.io.*;
// Main class class GFG {
public static void main(String[] args)
{
// Custom input string
String m = "heLLGFg" ;
String newstr = "" ;
String upper = "" ;
for ( int i = 0 ; i < m.length(); i++) {
if (Character.isUpperCase(m.charAt(i))) {
upper += m.substring(i, i + 1 );
}
else {
newstr += m.substring(i, i + 1 );
}
}
System.out.println(newstr + upper);
}
} |
hegLLGF