The Samantha Clause

Santa Claus is only one of many members of the Claus family. Each of the members of the family has a unique name, based on the following rules.

  • Each name can be obtained from the string SAMANTHA, by leaving out zero or more letters.
  • Each name contains at least three letters.
  • Each name contains at least one A.
  • No name contains two or three consecutive As.
  • No name contains more than three consecutive consonants.

For instance, valid names are SAM, SMAHA, MNTA, and ANH, but invalid are AM, SNT, SAATA, and AMNTH.

How many members does the Claus family have at most?


The answer is 128.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Arjen Vreugdenhil
Dec 14, 2015

We generate all 2 8 = 256 2^8 = 256 possible strings that can be obtained by leaving out letters, then check each string to see if it complies with all the rules.

However, this results in counting certain strings twice; e.g. SAHA can be made using either the first or the second A in SAMANTHA.

One way to avoid this is by keeping a list of generated names and discarding duplicates. I use a different strategy: when arriving at the letter A, if I skipped the previous A without including a letter in between, I discard the solution.

Technical detail/trick: The pattern of including/skipping letters is obtained from the bits of n n , which runs from 0000000 0 2 00000000_2 (empty string) to 1111111 1 2 11111111_2 (all of SAMANTHA).

 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
String s = "SAMANTHA";
int total = 0;

for (int n = 0; n < 256; n++) {
    boolean valid = true;
    boolean had_a = false, skip_a = false, last_a = false;
    int let_count = 0, cons_count = 0;

    for (int i = 0, k = n; i < 8; i++, k /= 2) {
        char c = s.charAt(i);

        if (k%2 == 0) {
            if (c == 'A') skip_a = true;
        }
        else {   
            let_count++;

            if (c == 'A') {
                if (skip_a || last_a) valid = false;
                had_a = true; cons_count = 0;
            }
            else {
                cons_count++; if (cons_count > 3) valid = false;
            }

            skip_a = false; last_a = (c == 'A');
        }
    }

    if (!had_a || let_count < 3) valid = false;

    if (valid) total ++;
}
out.println(total);

Output:

1
128

Can you please explain how "SMNAHA" is a valid name?

Arkin Dharawat - 5 years, 6 months ago

Log in to reply

My apologies-- it's not. That is a typo, and I will fix it.

Arjen Vreugdenhil - 5 years, 5 months ago

If you use all 8 letters, leaving out none, based on the rules you mentioned above you can make 2160 possible strings. If you include words with 3 to 7 letters then the possible strings increases to 7025. I am unable to understand the answer or the solution you provided. How can the answer be as low as 128? Perhaps I misunderstood the question. This is how I understood the question - find all unique strings with lengths ranging from 3 to 8, with the constraints as given above. Could you throw some light.

Siva Bathula - 5 years, 1 month ago

Log in to reply

"At least one A" and "no more than three consonants in a row" are stringent constraints!

Arjen Vreugdenhil - 5 years, 1 month ago

Log in to reply

With all 8 letters used, you have 18 ways to distribute the a's to fill up three of the slots and still be within the constraints and the rest consonants, can be arranged in 5! ways whichever slot they may be in. So its 18*5! = 2160 for 8 letters itself.

Siva Bathula - 5 years, 1 month ago

Log in to reply

@Siva Bathula Did you notice that the original order of the letters should be conserved? The problem states:

"... by leaving out zero or more letters"

not "by putting some of the letters in an arbitrary order".

Note also that in the examples, all letters have the same order as in SAMANTHA.

So you have only 2 8 2^8 possibilities to begin with, if you decide which letters to keep. Then you also have to apply the constraints.

Arjen Vreugdenhil - 5 years, 1 month ago

Log in to reply

@Arjen Vreugdenhil Right, it never stated arrangements, my bad.

Siva Bathula - 5 years, 1 month ago
Abdelhamid Saadi
Dec 19, 2015

Solution in python 3.5:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
lst = ['']

for k in "SAMANTHA":
    lst = lst + [x + k for x in lst]

tr = str.maketrans('SMNTH', 'XXXXX')

res = set([x for x in lst if len(x) >= 3 and x.find('A') >= 0 and x.find('AA')==-1 
       and x.translate(tr).find('XXXX') == -1])

print(len(res))

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...