java - How to transform a string such that it only contains vowels which have occurences equal or greater than their given values? -


i asked question in interview. have string s (all lowercase characters) need make new string p such :

  • the total number of a's in string should equal or greater given value of int variable a.
  • the total number of e's in string should equal or greater given value of int variable e.
  • the total number of i's in string should equal or greater given value of int variable i.
  • the total number of o's in string should equal or greater given value of int variable o.
  • the total number of u's in string should equal or greater given value of int variable u.

constraint in order resultant string, can replace character other lower case character. can number of times each replacement has cost associated it. cost of replacing c1 c2 absolute difference of ascii value of c1 , c2. eg. replacing b e has cost 3 , c a has cost 2.

we need find minimum cost of converting s p. given is:

  • string s
  • five ints - a, e, i, o, u

my approach first sort string, decrease given value of given int's each time encounter respective character, , whichever given variable greater 0, we'll start beginning of string , ig teh char not vowel, we'll calculate cost of replacing vowel of corresponding variable greater 0 , decrease it. doesn't seem work out. please let me know approach solve it. if information obscure, let me know in comments. thanks

here's code:

public class vowelcount {     static int mincount(string s, int a, int e, int i, int o, int u) {         char[] array = s.tochararray();         list<character> list = new arraylist<>();         list.add('a');         list.add('e');         list.add('i');         list.add('o');         list.add('u');          arrays.sort(array);          (int j = 0; j < s.length(); j++) {             if (s.charat(j) == 'a' && > 0) {                 a--;             }             else if (s.charat(j) == 'e' && > 0) {                 e--;             }             else if (s.charat(j) == 'i' && > 0) {                 i--;             }             else if (s.charat(j) == 'o' && > 0) {                 o--;             }             else if (s.charat(j) == 'u' && > 0) {                 u--;             }         }         int count =0;         (int j = 0; j < array.length; j++) {             int count1=0, count2=0, count3=0, count4=0, count5=0, semicount1=0, semicount2=0, fincount=0;             if (!list.contains(array[j])) {                 if (a > 0) {                     int c1 =(int) array[j];                     int c2 = (int) 'a';                     count1 = math.abs(c1-c2);                     a--;                 }                 if (e > 0) {                     int c1 =(int) array[j];                     int c2 = (int) 'e';                     count1 = math.abs(c1-c2);                     e--;                 }                 if (i > 0) {                     int c1 =(int) array[j];                     int c2 = (int) 'i';                     count1 = math.abs(c1-c2);                     i--;                 }                 if (o > 0) {                     int c1 =(int) array[j];                     int c2 = (int) 'o';                     count1 = math.abs(c1-c2);                     o--;                 }                 if (u > 0) {                     int c1 =(int) array[j];                     int c2 = (int) 'u';                     count1 = math.abs(c1-c2);                     u--;                 }                 if (count1!=0 && count2!=0) {                     semicount1 = math.min(count1, count2);                 } else if (count1 == 0) {                     semicount1 = count2;                 } else if (count2 == 0) {                     semicount1 = count1;                 }                 if (count3 != 0 && count4 != 0) {                     semicount2 = math.min(count3, count4);                 } else if (count3 == 0) {                     semicount2 = count4;                 } else if (count4 == 0) {                     semicount2 = count3;                 }                 system.out.println("adding count");                 fincount = math.min(semicount1, semicount2);                 if (count5 != 0) {                     count+= math.min(count5, fincount);                 } else count+= fincount;              }         }         system.out.println(count);         return count;     }      public static void main(string[] args) {         mincount("beiou", 1, 1, 1, 1 ,1);     } } 

i'm assuming sum of a, b, c, d , e @ s.length.

this problem can solved in linear time. before solving problem, lets consider 3 scenarios:

  • s = "bc", = 1, e = 1, best p = "ae", optimal cost = 1 + 2
  • s = "dj", = 1, e = 1, best p = "ae", optimal cost = 3 + 5
  • s = "fj", = 1, e = 1, best p = "ae", optimal cost = 5 + 5

we can think characters points in number line. in first scenario, consonants lie between vowels. in second one, on consonant lie between vowels , other 1 outside. in third example, both of them outside. , can see that, better choose vowel lower ascii key @ first , grab cheapest constant it.

so, approach be, convert constant 'a' need, in cheapest cost. go b, c , on.

edit: 1 particular case i've missed that, if we've 2 options choose same cost. 1 should choose? answer choose 1 lower ascii value. tushar makkar pointing out.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -