Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试在JavaScript中使用变音符号实现递归排列功能_Javascript_Recursion_Permutation_Substr_Backtracking - Fatal编程技术网

尝试在JavaScript中使用变音符号实现递归排列功能

尝试在JavaScript中使用变音符号实现递归排列功能,javascript,recursion,permutation,substr,backtracking,Javascript,Recursion,Permutation,Substr,Backtracking,我已经成功地编写了一个简单的函数,它接受一个输入字符串,并将每个字符与包含变音(重音)字符及其非变音字符配对的字典对象进行比较,然后尝试为该字符的每个排列创建一个新字符串 然而,它只是半功能性的。当我输入字符串“hello world”时,输出为: ["hèllo world", "héllo world", "hêllo world", "hëllo world", "hēllo world", "hėllo world", "hęllo world", "hellô world", "he

我已经成功地编写了一个简单的函数,它接受一个输入字符串,并将每个字符与包含变音(重音)字符及其非变音字符配对的字典对象进行比较,然后尝试为该字符的每个排列创建一个新字符串

然而,它只是半功能性的。当我输入字符串“hello world”时,输出为:

["hèllo world", "héllo world", "hêllo world", "hëllo world", "hēllo world", 
"hėllo world", "hęllo world", "hellô world", "hellö world", "hellò world", 
"helló world", "hellō world", "hellõ world", "hello wôrld", "hello wörld", 
"hello wòrld", "hello wórld", "hello wōrld", "hello wõrld"]
["śome string", "šome string", "sôme string", "söme string", "sòme string", 
"sóme string", "sōme string", "sõme string", "somè string", "somé string", 
"somê string", "somë string", "somē string", "somė string", "somę string", 
"some śtring", "some štring", "some strîng", "some strïng", "some stríng", 
"some strīng", "some strįng", "some strìng", "some striñg", "some strińg"]
或者如果我输入'some string',则输出为:

["hèllo world", "héllo world", "hêllo world", "hëllo world", "hēllo world", 
"hėllo world", "hęllo world", "hellô world", "hellö world", "hellò world", 
"helló world", "hellō world", "hellõ world", "hello wôrld", "hello wörld", 
"hello wòrld", "hello wórld", "hello wōrld", "hello wõrld"]
["śome string", "šome string", "sôme string", "söme string", "sòme string", 
"sóme string", "sōme string", "sõme string", "somè string", "somé string", 
"somê string", "somë string", "somē string", "somė string", "somę string", 
"some śtring", "some štring", "some strîng", "some strïng", "some stríng", 
"some strīng", "some strįng", "some strìng", "some striñg", "some strińg"]
然而,它所做的只是遍历每个字符并替换一次值,然后移动到下一个字符

如何对这些数组项中的每一项进行迭代,以找到它们的所有可能组合?而不是移动到下一个字符,并留下与原始输入字符串相同的字符。我需要它有每一个排列

我整晚都在努力想办法解决这个问题,虽然我取得了一些进展,但我还是被困在那里。我试图创建一个从内部调用自身的递归函数,但这使我的浏览器崩溃

任何帮助都将是惊人的:)

以下是我编写的代码:

function jig(inputStr) {
  const accents = {
    a: ["à", "á", "â", "ä", "ã", "å", "ā"],
    c: ["ç", "ć", "č"],
    e: ["è", "é", "ê", "ë", "ē", "ė", "ę"],
    i: ["î", "ï", "í", "ī", "į", "ì"],
    n: ["ñ", "ń"],
    o: ["ô", "ö", "ò", "ó", "ō", "õ"],
    s: ["ś", "š"],
    u: ["û", "ü", "ù", "ú", "ū"],
    y: ["ÿ"],
    z: ["ž", "ź", "ż"]
  };

  function hasAccents(char) {
    return /[aceinosuyz]/.test(char);
  }

  var results = [];

  for (var i = 0; i < inputStr.length; i++) {
    var currentChar = inputStr.substr(i, 1);
    // console.log(currentChar);
    if (hasAccents(currentChar)) {
      // console.log(accents[currentChar]);

      for (var y = 0; y < accents[currentChar].length; y++) {
        var tempArray = inputStr.split("");

        tempArray[i] = accents[currentChar][y];
        results.push(tempArray.join(""));
        //jig(tempArray.join(""));
      }
    }
  }

  return results;
}
函数夹具(inputStr){
常量重音={
a:[“a”、“a”、“–”、“ä”、“ã”、“å”、“ā”],
c:[“ç”、“ć”、“č”],
e:[“è”、“è”、“ē”、“ė”、“ę”],
i:[“î”、“ï”、“ī”、“į”、“ì”],
n:[“ñ”,“ń”],
o:[“ô”、“ö”、“ò”、“ó”、“ō”、“õ”],
s:[“ś”、“š”],
u:[“”、“ù”、“ù”、“ū”],
y:[“ÿ”],
z:[“ž”、“ź”、“ż”]
};
函数hasAccents(字符){
返回/[aceinosuyz]/.test(char);
}
var结果=[];
对于(变量i=0;i
下面是一个构建每个字符串的方法。当遇到重音的候选字符时,我们会将字符串的每个版本推到该字符的堆栈中

JavaScript代码:

函数f(s){
常量重音={
a:[“a”、“a”、“–”、“ä”、“ã”、“å”、“ā”],
c:[“ç”、“ć”、“č”],
e:[“è”、“è”、“ē”、“ė”、“ę”],
i:[“î”、“ï”、“ī”、“į”、“ì”],
n:[“ñ”,“ń”],
o:[“ô”、“ö”、“ò”、“ó”、“ō”、“õ”],
s:[“ś”、“š”],
u:[“”、“ù”、“ù”、“ū”],
y:[“ÿ”],
z:[“ž”、“ź”、“ż”]
};
var结果=[];
变量堆栈=['',0]];
while(堆栈长度){
让[str,i]=stack.pop();
如果(i==s.length){
结果:推(str);
继续;
}
if(重音符号[s[i]]){
对于(设j=0;j