V
V
Vladimir Rudometkin2018-12-21 01:31:32
Java
Vladimir Rudometkin, 2018-12-21 01:31:32

How is it possible to count repeated letters in a word?

How is it possible to count repeated letters in a word and display unique letters as ")", and repeated letters as "("?
For example:
weather - )()())
car - )()))(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2018-12-21
@TheKnight

If there are a limited number of letters, then you can use a bitset, encoding each letter into a bit index.
We go through, recalculate the letter into an index, check the current bit, output the character, write the bit.
Solved in one pass.
Instead of bitset, you can use an array of bits or an array of letter counts.
It also depends on what encoding the text is in, what variety of letters, and so on.
The Unicode table should fit in 20 kilobytes.
If we consider only Russian (English) letters in lower case, then this is 33 (26) bits of additional memory. Ignoring auxiliary wrappers. Or use UInt64 (UInt32 for English letters) as storage and check and expose bit magic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question