2017-05-22

magical vowels problems


Consider following letters : a, e, i, o, u

print Magical subsequence of the string that contains all five vowels in order. 

  • import java.util.regex.Matcher;
  • import java.util.regex.Pattern;

  • public class OnMobile {

  • public static int i=1;
  • static char getNext(){
  • i=i+1;
  •     String s2="aeiou";
  •     if(i>s2.length()){
  •         i=i-2;
  •     }
  •     else if(i==s2.length()){
  •         i=i-1;
  •     }
  •     return s2.charAt(i);
  •     
  • }
  •     static int longestSubsequence(String s) {
  •      Pattern pattern = Pattern.compile(".a.e.i.o.u.");
  •      Matcher matcher = pattern.matcher(s);
  •      boolean matchFound = matcher.matches();
  •      if(!matchFound){
  •      return 0;
  •      }
  •             int count=0;
  •             
  •             boolean start=true;
  •             char c1=' ';
  •             char c2=' ';
  •             for(int i=0;i<s.length();i++){
  •                 if(start){
  •                     if(s.charAt(i)=='a'){
  •                         c1='a';
  •                         c2='e';
  •                     start=false;
  •                     count=count+1;
  •                 }
  •                 }else {
  •                     if(s.charAt(i)==c1 || s.charAt(i)==c2){
  •                     if(s.charAt(i)==c2){
  •                        c1=c2;
  •                         c2=getNext();
  •                     }
  •                     count=count+1;
  •                 }}
  •             }
  •             return count;
  •     }


  • public static void main(String[] args) {
  • // TODO Auto-generated method stub
  • System.out.println(longestSubsequence("aeio"));
  • }

  • }


1 comment:

  1. this is not even right for the first test case. thanks.

    ReplyDelete