-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathAnagram.java
56 lines (42 loc) · 852 Bytes
/
Anagram.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
51
52
53
54
55
/*
Solved By: Sandeep Ranjan (1641012352)
*/
class Anagram {
static char[] arr;
static int size;
public static void main(String[] args) {
String input = "STOP";
size = input.length();
arr = new char[size];
for(int i=0; i<size; i++) {
arr[i] = input.charAt(i);
}
doAnagram(size);
}
public static void doAnagram(int newSize) {
if(newSize == 1) {
return;
}
for(int i=0; i<newSize; i++) {
doAnagram(newSize-1);
if(newSize==2) {
displayWord();
}
rotate(newSize);
}
}
public static void rotate(int newSize) {
int j;
int position = size - newSize;
char temp = arr[position];
for(j=position+1; j<size; j++)
arr[j-1] = arr[j];
arr[j-1] = temp;
}
public static void displayWord() {
for(int i=0; i<size; i++) {
System.out.print(arr[i]);
}
System.out.println();
}
}