35 lines
853 B
Text
35 lines
853 B
Text
import ballerina/io;
|
|
|
|
string[] finalRes = [];
|
|
|
|
function amb(string[][] wordsets, string[] res) returns boolean {
|
|
if wordsets.length() == 0 {
|
|
finalRes.push(...res);
|
|
return true;
|
|
}
|
|
string s = "";
|
|
int l = res.length();
|
|
if l > 0 { s = res[l - 1]; }
|
|
res.push("");
|
|
foreach string word in wordsets[0] {
|
|
res[l] = word;
|
|
if l > 0 && s[s.length() - 1] != res[l][0] { continue; }
|
|
if amb(wordsets.slice(1), [...res]) { return true; }
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function main() {
|
|
var wordsets = [
|
|
[ "the", "that", "a" ],
|
|
[ "frog", "elephant", "thing" ],
|
|
[ "walked", "treaded", "grows" ],
|
|
[ "slowly", "quickly" ]
|
|
];
|
|
|
|
if amb(wordsets, []) {
|
|
io:println(string:'join(" ", ...finalRes));
|
|
} else {
|
|
io:println("No amb found");
|
|
}
|
|
}
|