Log inSign up
Problems / B022

Counting tidy bracelets

200 ptsCases 15Set by dongbinna
Added October 30.

Statement

A workshop makes bracelets by threading colored beads in a line. The color of a bead is written as one lowercase English letter. One bracelet therefore shows up as a string made of lowercase letters only. For example, a bracelet written as bbacc has two beads of color b, one bead of color a, and two beads of color c threaded in that order.

The owner of the workshop calls a bracelet tidy when the beads of each color all sit together. In other words, if a color breaks off once and then appears again, the bracelet is not tidy.

For example, eezzzddwwwrrbbmxx is a tidy bracelet. The color changes at several places, but a color that has gone by never appears again. On the other hand, thzzt is not tidy, because t appears at the front, breaks off, and then appears again at the back.

Given the N bracelets in the workshop, write a program that counts how many of them are tidy.

For example, suppose the beads threaded on N=5 bracelets are as in the table below.

Bracelet12345
Beads threadedbdccafoovqqqxxxamluuuqqccfffyyjjdcdceae

Scanning each bracelet from the front and looking at the places where the color changes gives these verdicts.

Bracelet12345
VerdictTidyTidyTidyTidyNot tidy

Bracelet 5, dcdceae, starts with d, changes to c, and then shows d again. A color that had already gone by appeared again, so it is not tidy. On the other four, the beads of each color sit together in one place. So the answer in this case is 4.

A bracelet with only one bead has no place where the color changes, so it always counts as tidy. Bracelet 2 is such a case.

Input and Output Examples

Example 1

Input

5
bdcca
f
oovqqqxxxaml
uuuqqccfffyyjj
dcdceae

Output

4

Example 2

Input

1
eezzzddwwwrrbbmxx

Output

1

Example 3

Input

1
thzzt

Output

0

Input

  • First, the first line gives the number of bracelets N. N is a natural number between 1 and 100.

  • Next, over N lines, the beads threaded on each bracelet are given one per line. Each bracelet is made of lowercase English letters only, and its length is between 1 and 21. The number of beads threaded can differ from bracelet to bracelet.

Output

  • Print the number of tidy bracelets on one line. If there is no tidy bracelet at all, print 0. Do not attach a space after the value, and finish with one line break.

Requirements

  • Read the input from standard input and write the result to standard output
  • If a color breaks off and then appears again, the bracelet is not tidy
  • A bracelet with only one bead counts as tidy
  • Print the number of tidy bracelets only, on one line
  • You must write a program that reads from standard input and writes to standard output
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
  • There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 15 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
1
eezzzddwwwrrbbmxx
1
1
thzzt
0
5
bdcca
f
oovqqqxxxaml
uuuqqccfffyyjj
dcdceae
4

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 3.0ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 3 public cases. Submitting runs the hidden cases too.

People who solved this problem

Nobody yet. You could be the first.