r/dailyprogrammer • • Sep 30 '12
[9/30/2012] Challenge #102 [intermediate] (n-character-set strings)
Write a function that takes a string s and an integer n, and returns whether or not the string s contains at most n different characters.
For example, ncset("aacaabbabccc", 4) would return true, because it contains only 3 different characters, 'a', 'b', and 'c', and 3 ≤ 4.
For how many English words (yes, it's time for this dictionary again!) does ncset(word, 4) hold?
15
Upvotes
1
u/Wedamm Sep 30 '12
Haskell:
Am i missing something?
1
u/5outh 1 0 Sep 30 '12
Haskell:
Answer is:
2
u/skeeto -9 8 Sep 30 '12
In Common Lisp,
Output:
2
u/TimeWizid Oct 02 '12
Another go at it using C# and LINQ:
Here's the more readable version:
Here's the more efficient version. It stops counting as soon as it reaches n + 1 distinct characters:
2
u/takac00 Oct 02 '12
One line python
2
u/ilostmymangoman Sep 30 '12 edited Sep 30 '12
Python:
2
u/swarage 0 0 Sep 30 '12 edited Sep 30 '12
ruby solution :
1
u/[deleted] Sep 30 '12 edited Oct 02 '12
c++ v4
output:
1
u/yentup 0 0 Sep 30 '12
Python:
Output:
1
u/CMahaff Sep 30 '12 edited Sep 30 '12
Go Language
Outputs:
1
u/nagasgura 0 0 Sep 30 '12 edited Sep 30 '12
Python:
Output:
1
u/JerMenKoO 0 0 Sep 30 '12
Python:
>>>104421
u/prondose 0 0 Sep 30 '12 edited Sep 30 '12
Perl:
Usage:
1
u/[deleted] Sep 30 '12
c++, takes ~85 seconds
1
u/[deleted] Sep 30 '12
C
1
u/AgoAndAnon 0 1 Oct 01 '12
Here's another ruby solution, using set because I've programmed uniqueness tests so many times in the past. I use open-uri to grab the text file, because screw using the filesystem.
1
u/pivotallever Oct 01 '12
python
1
u/Miss_Moss Oct 01 '12 edited Oct 01 '12
C++ for great justice!
And just for fun, slightly compact version:
1
u/LiquidHelium Oct 02 '12
Clojure:
http://ideone.com/jC3AU
1
u/chaoticgeek Oct 03 '12
Here was my attempt in python.
Usage:
Output:
Not sure if I the output is correct although I did see someone with the same result.
1
u/Ledrug 0 2 Oct 04 '12
C
running:
1
u/groundhawk2006 Oct 06 '12 edited Oct 06 '12
Clojure:
And then, I loaded the dictionary into an array called words and applied this:
I got:
1
u/ThereminsWake Oct 07 '12
Python:
output:
1
u/darkgray Oct 07 '12
Go
Output
1
u/runvnc Oct 08 '12 edited Oct 08 '12
CoffeeScript:
Output:
1
u/[deleted] Oct 09 '12
1
u/WhiteSkyRising Oct 10 '12
My first submission. I'll take any and all advice. It needs better optimization; I have an array of 26 that keeps track of each letter in a word, then I have to scan them, then I have to set them all back to zero.
C:
1
u/spacemoses 1 1 Oct 11 '12
Lua, can't seem to get this very fast
Output:
1
u/larg3-p3nis Oct 13 '12
Java
1
u/mortisdeus Oct 18 '12
Python:
Solution:
1
u/[deleted] Nov 08 '12
JAVA
1
u/bin01 Nov 29 '12
Java:
}
1
u/marekkpie Jan 22 '13
Lua:
Output:
1
u/[deleted] Sep 30 '12
Verbose as always, but it works.
Java:
Output:
1
u/iMalevolence Sep 30 '12 edited Sep 30 '12
Java:
Prints:
1
u/willhaney Sep 30 '12 edited Oct 02 '12
C# LINQ
Output:
1
u/ixid 0 0 Sep 30 '12 edited Sep 30 '12
In the D language. The ncset method is quite fast, taking 90ms to get the answer below. It might be a little faster still using the popcnt assembler instruction to count the bits but my CPU is too old to have it.
Answer:
1
u/K1kuch1 Oct 01 '12
C
Ouput:
1
u/speakingcode Oct 01 '12
Java. I think this works, but I haven't tested it.
1
u/Medicalizawhat Oct 01 '12
Ruby:
1
u/oskar_stephens Oct 03 '12
Ruby:
0
u/tgkokk 0 0 Oct 03 '12
One liner Python:
Output is:
0
u/prometheusg Oct 03 '12
Lots of different ways to do this, but I wanted to use a BitSet to solve this one. My running time is about 100-130 ms. Is there a faster way? Using Java of course.
Java:
0
u/Sturmi12 Oct 06 '12
Java:
Usage:
Result: