Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Vowel Substring
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { System.out.println(vowelsubstring("aaeiouuu")); } /* * Complete the 'vowelsubstring' function below. * * The function is expected to return an INTEGER. * The function accepts STRING s as parameter. */ public static int vowelsubstring(String s) { // Write your code here System.out.println("String:"+s); int count = 0; int j=0; for(int i=0;i<=s.length()-5;i++){ if(isVowel(s.charAt(i))){ //Go upto the next consonent or end if(j<=i) j=i+1; for(;j<s.length();j++){ if(!isVowel(s.charAt(j))) break; } //check if substring has at least 5 characters if(j-i<5) continue; //count total valid strings in this substring count += countValidSubstrings(s.substring(i,j)); //take i to next vowel after j i=j; } } return count; } public static int countValidSubstrings(String substr){ int len = substr.length(); int count=0; /* Find first valid substring, * then count all valid substrings formed by combination * with rest of the letters. * i.e. count+=(len-j)+1 * Then go to the next letter and repeat the above step */ for(int i=0;i<=substr.length()-5;i++){ for(int j=i+5; j<=substr.length();j++){ if(isValidSubstring(substr.substring(i,j))){ count+=(len-j)+1; break; } } } return count; } //Check if a given substring has all the vowels at least once public static boolean isValidSubstring(String substr){ //System.out.println("checking substring:"+substr); if(substr.length()<5) return false; Set<Character> set = new HashSet<>(); set.add('a'); set.add('e'); set.add('i'); set.add('o'); set.add('u'); for(char c:substr.toCharArray()){ if(set.contains(c)) set.remove(c); } if(set.size()>0) return false; return true; } public static boolean isVowel(char c){ if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') return true; return false; } }
run
|
edit
|
history
|
help
0
exp6
Huffman Encoding Tree
calc exp nha
update
VENU DEVELOPMENTS
1.5
dimond of stars
Star
Borrowing class
Implementation of several common methods of LinkedList