{"id":211,"date":"2019-01-31T19:07:17","date_gmt":"2019-02-01T00:07:17","guid":{"rendered":"https:\/\/resrvoir.com\/?page_id=211"},"modified":"2019-03-17T13:49:13","modified_gmt":"2019-03-17T17:49:13","slug":"string-permutations","status":"publish","type":"page","link":"https:\/\/resrvoir.com\/?page_id=211","title":{"rendered":"String Permutations"},"content":{"rendered":"\n<p>Given a string, generate a list of all possible permutations of the string.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func permutations(string: String) -> [String] {\n  if string.count == 0 {\n    return [\"\"]\n  }\n  \n  var string = string\n  let char = string.removeFirst()\n\n  let strings = permutations(string: string)\n\n  var newStrings = [String]()\n  \n  for str in strings {\n    for i in 0 ... str.count {\n      var newString = str\n      \n      if i &lt; str.count {\n        let index = str.index(str.startIndex, offsetBy:i)\n        newString.insert(char, at: index)\n      }\n      else {\n        newString += String(char)\n      }\n      \n      newStrings.append(newString)\n    }\n  }\n  \n  return newStrings\n}\n\nlet p = permutations(string:\"abc\")\nprint(\"p \\(p)\")<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a string, generate a list of all possible permutations of the string.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":12,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/211"}],"collection":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=211"}],"version-history":[{"count":2,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/211\/revisions"}],"predecessor-version":[{"id":722,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/211\/revisions\/722"}],"up":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}