-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathPermutation.java
50 lines (49 loc) · 1.2 KB
/
Permutation.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;
/**
* @date 02/01/19
* @author SPREEHA DUTTA
*/
import java.util.*;
public class Permutation {
public static void permute(char []arr,int size,int n)
{
char b;int i;
if(size==1)
{
for(i=0;i<n;i++)
System.out.print(arr[i]);
System.out.println();
}
for(i=0;i<size;i++)
{
permute(arr,size-1,n);
if(size%2==1)
{
b=arr[0];
arr[0]=arr[size-1];
arr[size-1]=b;
}
else
{
b=arr[i];
arr[i]=arr[size-1];
arr[size-1]=b;
}
}
}
public static void main(String []args)
{
String s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter string");
s=sc.next();
char []arr=s.toCharArray();
System.out.println("The permutations are ");
permute(arr,arr.length,arr.length);
}
}