{"id":22513469,"url":"https://github.com/emahtab/permutations","last_synced_at":"2025-03-28T01:23:59.996Z","repository":{"id":79525708,"uuid":"219392020","full_name":"eMahtab/permutations","owner":"eMahtab","description":"Permutations","archived":false,"fork":false,"pushed_at":"2024-10-27T09:52:04.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:19:29.019Z","etag":null,"topics":["leetcode","permutations","problem-solving","recursion"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-04T01:24:28.000Z","updated_at":"2024-10-27T09:52:08.000Z","dependencies_parsed_at":"2023-05-10T17:30:28.298Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/permutations","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpermutations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpermutations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpermutations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpermutations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/permutations/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950670,"owners_count":20699107,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","permutations","problem-solving","recursion"],"created_at":"2024-12-07T03:12:26.297Z","updated_at":"2025-03-28T01:23:59.975Z","avatar_url":"https://github.com/eMahtab.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Permutations\n## https://leetcode.com/problems/permutations\n\nGiven a collection of **distinct** integers, return all possible permutations.\n```\nExample:\n\nInput: [1,2,3]\nOutput:\n[\n  [1,2,3],\n  [1,3,2],\n  [2,1,3],\n  [2,3,1],\n  [3,1,2],\n  [3,2,1]\n]\n```\n![Array Permutations](array-permutations.JPG?raw=true \"Array Permutations\")\n\n# Implementation : Time = O(n * n!)\n\n```java\nclass Solution {\n    public List\u003cList\u003cInteger\u003e\u003e permute(int[] nums) {\n    List\u003cList\u003cInteger\u003e\u003e result = new ArrayList\u003c\u003e();\n    permute(result, new ArrayList\u003cInteger\u003e(), nums);\n    return result;\n  }\n\n  private void permute(List\u003cList\u003cInteger\u003e\u003e result, List\u003cInteger\u003e permutation, int[] nums) {\n      if(nums.length == 0){\n        result.add(permutation);\n        return;\n      }\n      for(int i=0; i \u003c nums.length; i++){\n    \t List\u003cInteger\u003e runningPermutation = new ArrayList\u003cInteger\u003e(permutation);\n    \t runningPermutation.add(nums[i]);\n         permute(result, runningPermutation, subArray(nums, i));\n      }\n  }\n    \n  private int[] subArray(int[] input, int skipIndex) {\n      int subArraySize = input.length - 1;\n      int[] subArray = new int[subArraySize];\n      int index = 0;\n      for(int i = 0; i \u003c input.length; i++){\n          if(i != skipIndex)\n               subArray[index++] = input[i];\n      }\n      return subArray;\n  }  \n\n    \n}\n```\n\n### If we are given a String \n![String Permutations](string-permutations.JPG?raw=true \"String Permutations\")\n```java\npublic class Permutation {\n\tpublic static void main(String[] args) {\n\t\tpermutations(\"abc\");\n\t}\n\n\tpublic static void permutations(String str) {\n\t\tif (str == null || str.length() == 0) {\n\t\t\tSystem.out.println(\"Input string is empty !!\");\n\t\t\treturn;\n\t\t} else {\n\t\t\tpermute(\"\", str);\n\t\t}\n\t}\n\n\tpublic static void permute(String prefix, String remaining) {\n\t\tif (remaining.length() == 0) {\n\t\t\tSystem.out.println(prefix);\n\t\t\treturn;\n\t\t}\n\n\t\tfor (int i = 0; i \u003c remaining.length(); i++) {\n\t\t\tString newPrefix = prefix + remaining.charAt(i);\n\t\t\tString newRemaining = remaining.substring(0, i) + remaining.substring(i + 1);\n\t\t\tpermute(newPrefix, newRemaining);\n\t\t}\n\t}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fpermutations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fpermutations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fpermutations/lists"}