Currently just looking for a way to make this code faster.
This function currently takes in a string, s, and an int, k, and returns the substring of s length k with the most vowels.
Example:
s = caberqiitefg
k = 5
Output:
erqii
erqii is a substring of length 5 with the most vowels in s.
Returns “Not found!” if there are no vowels in the string.
function findSubstring(s, k)
local sub = string.sub
local gsub = string.gsub
local vowels = "[aeiou]"
local empty = ""
if #s - #gsub(s, vowels, empty) == 0 then
return "Not found!"
end
local mostVowels = nil
local vowelnum = 0
for i = 1,#s do
local curr = sub(s,i,k+i-1)
local currnum = #curr
if currnum == k then
local nvow = gsub(curr, vowels, empty)
if mostVowels == nil or (currnum-#nvow > vowelnum) then
mostVowels = curr
vowelnum = #mostVowels - #gsub(mostVowels, vowels, empty)
end
end
end
return mostVowels
end