Repository navigation
Expand file tree
/
Copy pathperm.java
More file actions
71 lines (58 loc) · 2.02 KB
/
Copy pathperm.java
File metadata and controls
71 lines (58 loc) · 2.02 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
package perm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Perm {
public static void main(String[] args) {
// prompt the user to enter their word
System.out.print("Please enter the word: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
// read the string from the command-line; need to use try/catch with
// the readLine() method
try {
str = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your word!");
System.exit(1);
}
System.out.println(findRank(str));
}
public static long findRank(String str) {
str = str.toUpperCase(); //ensure only capitalized.
long count = 1; //
int occurence = 0;
long rank = 1; // the permuted position of the given string.
// permutated container map for characters we previously looked at to
// find duplications.
Map<Character, Integer> charCounts = new HashMap<Character, Integer>();
// Here we iterate through all the given characters according to the
// given string's length.
for (int i = str.length() - 1; i > -1; i--) {
char currentCharacter = str.charAt(i); // the current character
// being assessed.
// have we already looked at this character? if exist add 1 else
// equals 1.
occurence = charCounts.containsKey(currentCharacter) ? charCounts
.get(currentCharacter) + 1 : 1;
charCounts.put(currentCharacter, occurence); // place the current
// character in our
// container.
// assess the current character against our container for
// duplicates/
for (Map.Entry<Character, Integer> e : charCounts.entrySet()) {
// Permutates through all the given values count!/occurrence!
// (n! / r!)
if (e.getKey() < currentCharacter) {
rank += count * e.getValue() / occurence;
}
}
count *= str.length() - i;
count /= occurence;
}
return rank;
}
}