Akamai Interview Question
Software DevelopersCountry: United States
My Java solution, complexity O(n), space O(n).
public String interchange(String s) {
String vowels = "aeiouy";
// array counter for count amount of each vowels in s
int[] counter = new int[300];
for (char c : s.toCharArray()) {
if (vowels.contains(c + "")) {
counter[c]++;
}
}
// build answer
StringBuilder builder = new StringBuilder();
for (char c : s.toCharArray()) {
// if c is vowels, search remain vowels in
// counter from 'a' to 'z' and append to answer
if (vowels.contains(c + "")) {
for (char x = 'a'; x <= 'z'; x++) {
// counter[x] > 0 mean vowels x is available
if (counter[x] > 0) {
builder.append(x);
counter[x]--;
break;
}
}
} else { // append other character to answer
builder.append(c);
}
}
return builder.toString();
}
My Java solution, complexity O(n), space O(n).
public String interchange(String s) {
String vowels = "aeiouy";
// array counter for count amount of each vowels in s
int[] counter = new int[300];
for (char c : s.toCharArray()) {
if (vowels.contains(c + "")) {
counter[c]++;
}
}
// build answer
StringBuilder builder = new StringBuilder();
for (char c : s.toCharArray()) {
// if c is vowels, search remain vowels in
// counter from 'a' to 'z' and append to answer
if (vowels.contains(c + "")) {
for (char x = 'a'; x <= 'z'; x++) {
// counter[x] > 0 mean vowels x is available
if (counter[x] > 0) {
builder.append(x);
counter[x]--;
break;
}
}
} else { // append other character to answer
builder.append(c);
}
}
return builder.toString();
}
/**
* Created by M on 7/10/17.
*/
import java.util.*;
public class VowelSwitcher {
public static void main (String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter the word you wish to change : ");
String word = in.next();
char [] splitWord = word.toCharArray();
boolean firstFound = false;
boolean secondFound = false;
int firstIndex=0,secondIndex=0;
HashSet<Character> vowels = new HashSet<Character>();
vowels.add('a');
vowels.add('A');
vowels.add('e');
vowels.add('E');
vowels.add('i');
vowels.add('I');
vowels.add('o');
vowels.add('O');
vowels.add('u');
vowels.add('U');
for (int i = 0; i < splitWord.length; i++ ){
if (vowels.contains(splitWord[i]) && firstFound && !secondFound){
secondFound = true;
secondIndex = i;
char temp = splitWord[firstIndex];
splitWord[firstIndex] = splitWord[secondIndex];
splitWord[secondIndex] = temp;
firstFound = false;
secondFound = false;
i++;
}
if (vowels.contains(splitWord[i]) && !firstFound){
firstFound = true;
firstIndex = i;
}
}
System.out.println(splitWord);
}
}
public String interchange(String s) {
String vowels = "aeiouy";
// array counter for count amount of each vowels in s
int[] counter = new int[300];
for (char c : s.toCharArray()) {
if (vowels.contains(c + "")) {
counter[c]++;
}
}
// build answer
StringBuilder builder = new StringBuilder();
for (char c : s.toCharArray()) {
// if c is vowels, search remain vowels in
// counter from 'a' to 'z' and append to answer
if (vowels.contains(c + "")) {
for (char x = 'a'; x <= 'z'; x++) {
// counter[x] > 0 mean vowels x is available
if (counter[x] > 0) {
builder.append(x);
counter[x]--;
break;
}
}
} else { // append other character to answer
builder.append(c);
}
}
return builder.toString();
}
-----
This code is not working for the below test cases:
Input: snahe
Output: snahe
Expected: sneha
Input: bashun
Output: bashun
Expected: bushan
if the vowels in order, then it will not interchange.
In our first input sneha, we expecting a should interchange with e and vice versa
public String interchange(String s) {
String vowels = "aeiouy";
// array counter for count amount of each vowels in s
int[] counter = new int[300];
for (char c : s.toCharArray()) {
if (vowels.contains(c + "")) {
counter[c]++;
}
}
// build answer
StringBuilder builder = new StringBuilder();
for (char c : s.toCharArray()) {
// if c is vowels, search remain vowels in
// counter from 'a' to 'z' and append to answer
if (vowels.contains(c + "")) {
for (char x = 'a'; x <= 'z'; x++) {
// counter[x] > 0 mean vowels x is available
if (counter[x] > 0) {
builder.append(x);
counter[x]--;
break;
}
}
} else { // append other character to answer
builder.append(c);
}
}
return builder.toString();
}
/*
This program is not working for the below test cases:
Input : snahe
Output : snahe
Expected Output : sneha
Input : bashun
Output : bashun
Expected Output : bushan
If the vowels is in order in a string, then interchange will not happen.
In our first test case, we are expecting a should interchange with e.
*/
There are cleaner solutions. Suitably change it into Java.
def reverse_vowels_declarative(word){
vowels = set( 'aeiou'.value ) // get into a set
vowels_in_word = select( word.value ) where { $.o @ vowels }
// put back
fold ( word.value , -1 ) as {
continue( !($.o @ vowels) )
word.value[$.i] = vowels_in_word[$.p]
$.p -= 1
}
word
}
def reverse_vowels_imperative(word){
vowels = set( 'aeiou'.value ) // get into a set
lp = 0 ; rp = size(word) - 1
while ( true ){
while ( lp < rp && !(word[lp] @ vowels) ){ lp += 1 }
while ( lp < rp && !(word[rp] @ vowels) ){ rp -= 1 }
break(lp >= rp )
t = word[rp]
word.value[rp] = word[lp]
word.value[lp] = t
lp +=1 ; rp -= 1
}
word // done
}
println( reverse_vowels_imperative('vowels') )
import java.util.HashSet;
import java.util.Set;
//@author: Deepak Kumar Modi (from Wibmo Software)
public class ExchangeVowel {
public static void main(String[] args) {
String x = "vowels";
//String x = "sneha";
//String x = "continuous";
StringBuilder y = new StringBuilder(x);
Set<Character> set = new HashSet<Character>();
set.add('a');
set.add('e');
set.add('i');
set.add('o');
set.add('u');
String result="";
Character temp1=' ';
Character temp2=' ';
int index=0;
for(int i=0;i<x.length();i++){
if(set.contains(x.charAt(i))){
if(index>0) {
y.setCharAt(index, x.charAt(i));
}
if(temp1!=' ')
temp2 = temp1;
temp1 = x.charAt(i);
index = i;
y.setCharAt(index, temp2);
}
}
System.out.println(y.toString());
}
}
import java.util.HashSet;
import java.util.Set;
//@author: Deepak Kumar Modi (from Wibmo Software)
public class ExchangeVowel {
public static void main(String[] args) {
String x = "vowels";
//String x = "sneha";
//String x = "continuous";
StringBuilder y = new StringBuilder(x);
Set<Character> set = new HashSet<Character>();
set.add('a');
set.add('e');
set.add('i');
set.add('o');
set.add('u');
String result="";
Character temp1=' ';
Character temp2=' ';
int index=0;
for(int i=0;i<x.length();i++){
if(set.contains(x.charAt(i))){
if(index>0) {
y.setCharAt(index, x.charAt(i));
}
if(temp1!=' ')
temp2 = temp1;
temp1 = x.charAt(i);
index = i;
y.setCharAt(index, temp2);
}
}
System.out.println(y.toString());
}
}
public class VowelsInterchange {
public void Vowels(String str) {
List<Character> vowels = new ArrayList<Character>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
HashMap<Integer, Integer> hm = new HashMap<>();
Integer fIndex = -1;
for (int i = 0; i < str.length(); i++) {
if (vowels.contains(str.charAt(i))) {
if (fIndex != -1) {
hm.put(fIndex, i);
fIndex = -1;
} else {
fIndex = i;
}
}
}
SwapCharacters(str, hm);
}
public void SwapCharacters(String str, HashMap<Integer, Integer> hm) {
String fSub = "";
String sSub = "";
String tSub = "";
char fChar = ' ';
char sChar = ' ';
int fIndex = 0;
int sIndex = 0;
for (Map.Entry<Integer, Integer> entry : hm.entrySet()) {
fIndex = entry.getKey();
sIndex = entry.getValue();
if (fIndex > 0)
fSub = str.substring(0, fIndex);
if (sIndex - fIndex > 1)
sSub = str.substring(fIndex + 1, sIndex);
if (sIndex < (str.length() - 1))
tSub = str.substring(sIndex + 1, str.length());
fChar = str.charAt(fIndex);
sChar = str.charAt(sIndex);
str = fSub + sChar + sSub + fChar + tSub;
fSub = "";
sSub = "";
tSub = "";
}
System.out.println(str);
}
}
import java.util.HashSet;
import java.util.Set;
public class VowelsSwitcher {
public static void main(String[] args) {
Set<Character> vowels = new HashSet<Character>();
"aeiou".chars().forEach(i -> vowels.add((char) i));
String str = "continuos";
char [] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if(vowels.contains(c)) {
int j = i+1;
while(j < (chars.length-i)) {
j++;
if(vowels.contains(chars[j])) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i = j;
break;
}
}
}
}
System.out.println("Orginal String -> "+str);
System.out.println("After switching -> "+ new String(chars));
}
}
import java.util.HashSet;
import java.util.Set;
public class VowelsSwitcher {
public static void main(String[] args) {
Set<Character> vowels = new HashSet<Character>();
"aeiouAEIOU".chars().forEach(i -> vowels.add((char) i));
String str = "continuos";
char [] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if(vowels.contains(c)) {
int j = i+1;
while(j < (chars.length-i)) {
j++;
if(vowels.contains(chars[j])) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i = j;
break;
}
}
}
}
System.out.println("Orginal String -> "+str);
System.out.println("After switching -> "+ new String(chars));
}
}
- Anonymous July 11, 2017