How to transform a string such that it only contains vowels which have occurences equal or greater than their given values?
<p>I was asked this question in an interview. We have a string <code>s</code> (all lowercase characters) from which we need to make a new string <code>p</code> such that :</p>

<ul>
<li>The total number of <em>a</em>'s in the string should be equal or greater than the given value of an int variable <code>a</code>.</li>
<li>The total number of <em>e</em>'s in the string should be equal or greater than the given value of an int variable <code>e</code>.</li>
<li>The total number of <em>i</em>'s in the string should be equal or greater than the given value of an int variable <code>i</code>.</li>
<li>The total number of <em>o</em>'s in the string should be equal or greater than the given value of an int variable <code>o</code>.</li>
<li>The total number of <em>u</em>'s in the string should be equal or greater than the given value of an int variable <code>u</code>.</li>
</ul>

<p>Constraint was the in order to get the resultant string, we can replace any character with any other lower case character. We can do this any number of times but each replacement has a cost associated with it. the cost of replacing <code>c1</code> with <code>c2</code> is the <em>absolute difference of ASCII value of <code>c1</code> and <code>c2</code></em>. For eg. replacing <em>b</em> with <em>e</em> has a cost 3 and <em>c</em> with <em>a</em> has a cost 2.</p>

<p>We need to find the <strong>minimum cost</strong> of converting s into p. Given is:</p>

<ul>
<li>String s</li>
<li>Five ints - a, e, i, o, u</li>
</ul>

<p>My approach was to first sort the string, then decrease the given value of given int's each time we encounter the respective character, and then whichever given variable is greater than 0, we'll start from the beginning of the string and ig teh char is not a vowel, we'll calculate the cost of replacing it with the vowel of which corresponding variable is greater than zero and decrease it. It doesn't seem to work out. 
Please let me know your approach to solve it. If any information is obscure, let me know in comments. Thanks</p>

<p>here's my code:</p>

<pre><code>public class VowelCount {
    static int minCount(String s, int a, int e, int i, int o, int u) {
        char[] array = s.toCharArray();
        List&lt;Character&gt; list = new ArrayList&lt;&gt;();
        list.add('a');
        list.add('e');
        list.add('i');
        list.add('o');
        list.add('u');

        Arrays.sort(array);

        for (int j = 0; j &lt; s.length(); j++) {
            if (s.charAt(j) == 'a' &amp;&amp; a &gt; 0) {
                a--;
            }
            else if (s.charAt(j) == 'e' &amp;&amp; a &gt; 0) {
                e--;
            }
            else if (s.charAt(j) == 'i' &amp;&amp; a &gt; 0) {
                i--;
            }
            else if (s.charAt(j) == 'o' &amp;&amp; a &gt; 0) {
                o--;
            }
            else if (s.charAt(j) == 'u' &amp;&amp; a &gt; 0) {
                u--;
            }
        }
        int count =0;
        for (int j = 0; j &lt; 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 &gt; 0) {
                    int c1 =(int) array[j];
                    int c2 = (int) 'a';
                    count1 = Math.abs(c1-c2);
                    a--;
                }
                if (e &gt; 0) {
                    int c1 =(int) array[j];
                    int c2 = (int) 'e';
                    count1 = Math.abs(c1-c2);
                    e--;
                }
                if (i &gt; 0) {
                    int c1 =(int) array[j];
                    int c2 = (int) 'i';
                    count1 = Math.abs(c1-c2);
                    i--;
                }
                if (o &gt; 0) {
                    int c1 =(int) array[j];
                    int c2 = (int) 'o';
                    count1 = Math.abs(c1-c2);
                    o--;
                }
                if (u &gt; 0) {
                    int c1 =(int) array[j];
                    int c2 = (int) 'u';
                    count1 = Math.abs(c1-c2);
                    u--;
                }
                if (count1!=0 &amp;&amp; count2!=0) {
                    semicount1 = Math.min(count1, count2);
                } else if (count1 == 0) {
                    semicount1 = count2;
                } else if (count2 == 0) {
                    semicount1 = count1;
                }
                if (count3 != 0 &amp;&amp; count4 != 0) {
                    semicount2 = Math.min(count3, count4);
                } else if (count3 == 0) {
                    semicount2 = count4;
                } else if (count4 == 0) {
                    semicount2 = count3;
                }
                System.out.println("adding to 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);
    }
}
</code></pre>