MadMax1028

Longest vowel

Jun 12th, 2020
1,117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun main() {
  2.     val validSequenceLetters = charArrayOf('a', 'e', 'i', 'o', 'u')
  3.    
  4.     fun CharSequence.startsAndEndsWithCorrectLetter(): Boolean {
  5.         return firstOrNull() == validSequenceLetters.first() && last() == validSequenceLetters.last()
  6.     }
  7.    
  8.     val string = "dfgaeioozuaeiooua"
  9.    
  10.     val sb = StringBuilder()
  11.     var longestVowel = 0;
  12.    
  13.     for (char in string) {
  14.         if (sb.isNotEmpty() && (sb.last() > char || char !in validSequenceLetters)) {
  15.             if (sb.startsAndEndsWithCorrectLetter()) {
  16.                 longestVowel = sb.length
  17.             }
  18.             sb.clear()
  19.         }
  20.         sb.append(char)
  21.     }
  22.     if (sb.startsAndEndsWithCorrectLetter()) {
  23.         longestVowel = sb.length
  24.     }
  25.    
  26.     println(longestVowel)
  27. }
RAW Paste Data