-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRandomWordMaker
More file actions
124 lines (100 loc) · 3.43 KB
/
RandomWordMaker
File metadata and controls
124 lines (100 loc) · 3.43 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 🎲 Random Word Maker
A Java program that generates all possible unique random permutations of a given word. This program uses a `HashSet` to ensure no duplicate words are generated and calculates the maximum number of permutations using the factorial of the word's length.
## 🚀 Features
- ✅ Generates all unique random permutations of a word.
- ✅ Ensures no duplicate permutations using a `HashSet`.
- ✅ Calculates the maximum number of permutations using factorial logic.
- ✅ Outputs the generated permutations in uppercase.
## 🛠️ How It Works
1. The user inputs a word.
2. The program calculates the factorial of the word's length to determine the maximum number of unique permutations.
3. Random permutations are generated using a `Random` object and stored in a `HashSet` to avoid duplicates.
4. All unique permutations are printed to the console.
## 📂 File Structure
```
RandomWordMaker.java
```
## 📋 Example Usage
### Input:
```
Enter a word: JAVA
```
### Output:
```
Unique random words generated from the input word:
1 AVJA
2 JAAJ
3 AJVA
4 VAJA
5 AJAV
6 AVAJ
...
```
## 🧮 How Factorial is Used
The program calculates the factorial of the word's length (`n!`) to determine the maximum number of unique permutations. For example:
- A word with 3 letters (`ABC`) has `3! = 6` unique permutations: `ABC, ACB, BAC, BCA, CAB, CBA`.
## 📦 Requirements
- Java Development Kit (JDK) 8 or higher.
## 🏃♂️ How to Run
1. Clone the repository:
```bash
git clone https://github.com/Warlord27/JavaProb.git
```
2. Navigate to the project directory:
```bash
cd random-word-maker
```
3. Compile the program:
```bash
javac RandomWordMaker.java
```
4. Run the program:
```bash
java RandomWordMaker
```
Code👩💻:
import java.util.Scanner;
import java.util.Random;
import java.util.HashSet;
public class RandomWordMaker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String wor = sc.next().toUpperCase();
int c = wor.length();
Random rG = new Random();
HashSet<String> WordList = new HashSet<>();
// Generate all possible unique random words
while (WordList.size() < factorial(c)) {
StringBuilder randomWord = new StringBuilder(wor);
StringBuilder Shuf = new StringBuilder();
while (randomWord.length() > 0) {
int randomIndex = rG.nextInt(randomWord.length());
Shuf.append(randomWord.charAt(randomIndex));
randomWord.deleteCharAt(randomIndex);
}
WordList.add(Shuf.toString());
}
// Print all unique random words
System.out.println("Unique random words generated from the input word:");
int count = 1;
for (String word : WordList) {
System.out.println(count + " " + word);
count++;
}
}
// Helper method to calculate factorial
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
}
## 🤝 Contributing
Feel free to fork this repository and submit pull requests. Contributions are welcome! 🎉
## 📜 License
This project is licensed under the MIT License. See the `LICENSE` file for details.
---
Made with 😁 by [Adam Warlord](https://github.com/Warlord27)
```