Given a 0-indexed string s, permute s to get a new string t such that: All consonants…
- User
Given a 0-indexed string s, permute s to get a new string t such that:
All consonants remain in their original places.
More formally, if there is an index i with 0 <= i << s.length such that s[i] is a consonant, then t[i] = s[i].
The vowels must be sorted in
the nondecreasing order of their ASCII values. More formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j].
Return the resulting string.
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.
Sample Input Format
s = "1Eetcode"
Sample Output Format
"1EOtcede" Give me the code in java using scanner
Assistant
Here's the Java code to solve this problem using Scanner:
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class SortVowelsInString { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read the input string String s = sc.nextLine(); // Close the scanner sc.close(); // Get the result and print it String result = sortVowels(s); System.out.println(result); } public static String sortVowels(String s) { // Convert string to character array for easier manipulation char[] chars = s.toCharArray(); // List to store vowels found in the string ArrayList<Character> vowels = new ArrayList<>(); // Set of vowels for quick lookup String vowelSet = "aeiouAEIOU"; // Extract all vowels from the string for (char c : chars) { if (vowelSet.indexOf(c) != -1) { vowels.add(c); } } // Sort vowels by ASCII value Collections.sort(vowels); // Build the result string StringBuilder result = new StringBuilder(); int vowelIndex = 0; for (char c : chars) { if (vowelSet.indexOf(c) != -1) { // If current character is a vowel, replace with sorted vowel result.append(vowels.get(vowelIndex)); vowelIndex++; } else { // If consonant, keep it as is result.append(c); } } return result.toString(); } }How the Algorithm Works:
-
Identify and Extract Vowels: We iterate through the string and collect all vowels (both lowercase and uppercase) into a separate list.
-
Sort Vowels: We sort the collected vowels based on their ASCII values. In ASCII:
- Uppercase letters come before lowercase letters
- 'A' = 65, 'E' = 69, 'I' = 73, 'O' = 79, 'U' = 85
- 'a' = 97, 'e' = 101, 'i' = 105, 'o' = 111, 'u' = 117
-
Reconstruct String: We iterate through the original string again:
- If the character is a consonant, we keep it in its original position
- If the character is a vowel, we replace it with the next vowel from our sorted list
Example Walkthrough:
For input
"lEetcode":- Extract vowels: ['E', 'e', 'o', 'e']
- Sort vowels: ['E', 'e', 'e', 'o'] (uppercase 'E' comes before lowercase 'e')
- Reconstruct:
- 'l' → consonant, keep 'l'
- 'E' → vowel, replace with 'E' (first from sorted list)
- 'e' → vowel, replace with 'e' (second from sorted list)
- 't' → consonant, keep 't'
- 'c' → consonant, keep 'c'
- 'o' → vowel, replace with 'e' (third from sorted list)
- 'd' → consonant, keep 'd'
- 'e' → vowel, replace with 'o' (fourth from sorted list)
- Result: "lEetcedo"
How to Run:
- Save the code as
SortVowelsInString.java - Compile:
javac SortVowelsInString.java - Run:
java SortVowelsInString - Enter your string when prompted
Sample Input:
lEetcodeSample Output:
lEetcedoThe solution has a time complexity of O(n log n) due to sorting, where n is the number of vowels in the string.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.