Ganesh1648

Subsequence

Jun 14th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.math.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class Codechef {
  8.  
  9.  
  10.   static String str;
  11.   static int max_len=0;
  12.   static int max_sum=0;
  13.   static String res="";
  14.   static int character(char c)
  15.   {
  16.       if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
  17.         return 1;
  18.       else
  19.         return 0;
  20.   }
  21.   static void solve(int cur,int prev,int len,int sum,String st) {
  22.    
  23.        if(cur>=str.length())
  24.        {
  25.            if(max_len<len)
  26.            {
  27.                max_sum=sum;
  28.                max_len=len;
  29.                res=st;
  30.            }
  31.            else if(max_len==len)
  32.            {
  33.                if(max_sum<sum){
  34.                 max_sum=sum;
  35.                 res=st;  
  36.                }
  37.            }
  38.            return;
  39.        }
  40.        if(prev==-1)
  41.        {
  42.            solve(cur+1,cur,len+1,sum+str.charAt(cur)-'a',st+""+str.charAt(cur));
  43.            solve(cur+1,prev,len,sum,st);
  44.        }
  45.        else if(character(str.charAt(prev))!=character(str.charAt(cur)))
  46.        {
  47.            solve(cur+1,cur,len+1,sum+str.charAt(cur)-'a',st+""+str.charAt(cur));
  48.            solve(cur+1,prev,len,sum,st);
  49.        }
  50.        else
  51.             solve(cur+1,prev,len,sum,st);
  52.     }
  53.  
  54.   public static void main(String[] args) throws java.lang.Exception {
  55.     BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  56.     str=bf.readLine();
  57.     solve(0,-1,0,0,"");
  58.     System.out.println(res);
  59.   }
  60. }
Add Comment
Please, Sign In to add comment