Created
April 11, 2023 23:36
-
-
Save truemedian/5594a9528c51f188242d32e3d212cffe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ffi = require 'ffi' | |
local function permute_inner(arr, l, r) | |
if l == r then | |
coroutine.yield(ffi.string(arr)) | |
else | |
for i = l, r do | |
arr[l], arr[i] = arr[i], arr[l] | |
permute_inner(arr, l + 1, r) | |
arr[l], arr[i] = arr[i], arr[l] | |
end | |
end | |
end | |
local function permute(str) | |
local arr = ffi.new('char[?]', #str + 1, str) | |
return coroutine.wrap(function() permute_inner(arr, 0, #str - 1) end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment