(Hacker Rank) Consonant Reversal - Solution
Link to the problem: https://www.hackerrank.com/contests/womens-codesprint/challenges/consonant-reversal
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String vowels = "aeiou";
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++) {
String line = sc.next();
int last = line.length()-1;
for (int j = 0; j < line.length(); j++) {
if (vowels.indexOf(line.charAt(j)) > -1) {
System.out.print(line.charAt(j));
}
else {
for (int k = last; k >= 0; k--) {
if (vowels.indexOf(line.charAt(k)) == -1) {
System.out.print(line.charAt(k));
last = k-1;
break;
}
}
}
}
System.out.println();
}
}
}
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String vowels = "aeiou";
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++) {
String line = sc.next();
int last = line.length()-1;
for (int j = 0; j < line.length(); j++) {
if (vowels.indexOf(line.charAt(j)) > -1) {
System.out.print(line.charAt(j));
}
else {
for (int k = last; k >= 0; k--) {
if (vowels.indexOf(line.charAt(k)) == -1) {
System.out.print(line.charAt(k));
last = k-1;
break;
}
}
}
}
System.out.println();
}
}
}
Comments
Post a Comment