function Factorial(Number)
    return (Number == 0 and 1) or (Number * Factorial(Number - 1))
end
function Permute(String)
    local Length = #String
    local TotalPermutes = Factorial(Length)
    
    local Permutations = {}
    
    for Index = 1, TotalPermutes do
        print(Index)
        
        table.insert(Permutations, String)
        
        local Index2 = Length - 1
        
        while String:sub(Index2, Index2) >= String:sub(Index2 + 1, Index2 + 1) do
            Index2 = Index2 - 1
        end
        
        local Length2 = Length
        
        while String:sub(Index2, Index2) >= String:sub(Length2, Length2) do
            Length2 = Length2 - 1
        end
        
        String = SwapCharacters(String, Index2, Length2)
        String = ReverseSubstring(String, Index2 + 1)
    end
    
    return Permutations
end
function SwapCharacters(String, Index, Index2)
    local Characters = {}
    
    for Character in String:gmatch(".") do
        table.insert(Characters, Character)
    end
    
    Characters[Index], Characters[Index2] = Characters[Index2], Characters[Index]
    
    return table.concat(Characters)
end
function ReverseSubstring(String, Start)
    local Characters = {}
    
    for Character in String:gmatch(".") do
        table.insert(Characters, Character)
    end
    
    local Index = Start
    local Index2 = #Characters
    
    while Index < Index2 do
        Characters[Index], Characters[Index2] = Characters[Index2], Characters[Index]
        
        Index = Index + 1
        Index2 = Index2 - 1
    end
    
    return table.concat(Characters)
end
local String = ""
local Permutations = Permute("wow")
for _, Permute in next, Permutations do
    print(Permute)
end