Imagine an alphabet of words.
Example:
a ==> 1
b ==> 2
c ==> 3
z ==> 26
ab ==> 27
ac ==> 28
az ==> 51
bc ==> 52
This really ought to be a comment, but I can't put code in a comment.
I wrote a brute force program to calculate the number of one, two, three, four, and five letter words, based on the criteria the original poster provided.
Imagine an alphabet of words such that the sequence of characters in a word has to be in ascending order only.
Here are the results of my program.
One letter words - 26
Two letter words - 325
Three letter words - 2600
Four letter words - 14950
Five letter words - 65780
Total words - 83681
My "solution" would be to generate a dictionary of all the words from a to abcdefghijklmnopqrstuvwxyz.
Here's the code I used. Maybe someone can look at the nested loops and come up with a formula. I can't.
public class WordSequence implements Runnable {
private int wordCount = 0;
@Override
public void run() {
int count = createOneLetterWords();
System.out.println("One letter words - " + count);
count = createTwoLetterWords();
System.out.println("Two letter words - " + count);
count = createThreeLetterWords();
System.out.println("Three letter words - " + count);
count = createFourLetterWords();
System.out.println("Four letter words - " + count);
count = createFiveLetterWords();
System.out.println("Five letter words - " + count);
System.out.println("\nTotal words - " + wordCount);
}
private int createOneLetterWords() {
int count = 0;
for (int i = 0; i < 26; i++) {
createWord(i);
wordCount++;
count++;
}
return count;
}
private int createTwoLetterWords() {
int count = 0;
for (int i = 0; i < 25; i++) {
for (int j = i + 1; j < 26; j++) {
createWord(i, j);
wordCount++;
count++;
}
}
return count;
}
private int createThreeLetterWords() {
int count = 0;
for (int i = 0; i < 24; i++) {
for (int j = i + 1; j < 25; j++) {
for (int k = j + 1; k < 26; k++) {
createWord(i, j, k);
wordCount++;
count++;
}
}
}
return count;
}
private int createFourLetterWords() {
int count = 0;
for (int i = 0; i < 23; i++) {
for (int j = i + 1; j < 24; j++) {
for (int k = j + 1; k < 25; k++) {
for (int m = k + 1; m < 26; m++) {
createWord(i, j, k, m);
wordCount++;
count++;
}
}
}
}
return count;
}
private int createFiveLetterWords() {
int count = 0;
for (int i = 0; i < 22; i++) {
for (int j = i + 1; j < 23; j++) {
for (int k = j + 1; k < 24; k++) {
for (int m = k + 1; m < 25; m++) {
for (int n = m + 1; n < 26; n++) {
createWord(i, j, k, m, n);
wordCount++;
count++;
}
}
}
}
}
return count;
}
private String createWord(int... letter) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < letter.length; i++) {
builder.append((char) (letter[i] + 'a'));
}
return builder.toString();
}
public static void main(String[] args) {
new WordSequence().run();
}
}