GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaProgramming/Recursion/Permutations.java at main · pradeepkumar24rk/JavaProgramming Expand file tree/ Copy pathPermutations.javaMore file actions 79 lines (70 loc) · 2.44 KB / Copy pathPermutations.javaFile metadata and controls79 lines (70 loc) · 2.44 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 package Recursion; import java.util.ArrayList; import java.util.List; public class Permutations { private static void permutation(String str,String ans) { if(str.length() == 0) { System.out.println(ans); return; } char ch = str.charAt(0); for(int i = 0;i<=ans.length();i++) { String f = ans.substring(0, i); String e = ans.substring(i,ans.length()); permutation(str.substring(1), f+ch+e); } } private static ArrayList<String> permutationArrayList(String str,String ans) { if(str.length() == 0) { ArrayList<String> list = new ArrayList<>(); list.add(ans); return list; } char ch = str.charAt(0); ArrayList<String> arr = new ArrayList<>(); for(int i = 0;i<=ans.length();i++) { String f = ans.substring(0, i); String e = ans.substring(i,ans.length()); arr.addAll(permutationArrayList(str.substring(1), f+ch+e)); } return arr; } private static int permutationCount(String str,String ans) { if(str.length() == 0) { // System.out.println(ans); return 1; } int count = 0; char ch = str.charAt(0); for(int i = 0;i<=ans.length();i++) { String f = ans.substring(0, i); String e = ans.substring(i,ans.length()); count+=permutationCount(str.substring(1), f+ch+e); } return count; } // Array Permutation static void permutationArray(int[] nums,List<List<Integer>> list,List<Integer> ans,int index) { if(index == nums.length){ list.add(ans); return; } for(int i = 0;i<=ans.size();i++){ List<Integer> temp = new ArrayList<>(ans) ; temp.add(i,nums[index]); permutationArray(nums,list,temp,index+1); } } public static List<List<Integer>> Arraypermute(int[] nums) { List<List<Integer>> list = new ArrayList<>(); permutationArray(nums,list,new ArrayList<Integer>(),0); return list; } public static void main(String[] args) { String str = "abc"; permutation(str,""); System.out.println(permutationArrayList(str,"")); System.out.println(permutationCount("abcd","")); System.out.println(Arraypermute(new int[] {1,2,3})); } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |