In this challenge, we will check if a string contains consecutive letters as they appear in the English alphabet and if each letter occurs only once.
Rules are: (1) the letters are adjacent in the English alphabet, and (2) each letter occurs only once.
For example:
solve("abc")
= True, because it contains a,b,c
solve("abd")
= False, because a, b, d are not consecutive/adjacent in the alphabet, and c is missing.
solve("dabc")
= True, because it contains a, b, c, d
solve("abbc")
= False, because b does not occur once.
solve("v")
= True
All inputs will be lowercase letters.
Tests
solve("abc")
solve("zyx")
solve("azj")
Good luck!
This challenge comes from KenKemau on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (12)
JS
It's always nice to avoid a second iteration over elements if possible and efficient. This could allow a function to be applied on a stream/very-large-string, makes analysis of it easier, and just avoids having it do unnecessary work on elements it doesn't strictly need to deal with.
Here's a single loop TS implementation that will early exit as soon as it finds an element that...
If the whole string has been covered and the call to
every
didn't early exit, then all letters must be distinct (rule two covered), and the max code minus the min code must be less than the length of the string (they're all different to each other so this bound means they must be adjacent, rule one covered)Tested in kata
Edit: refactor to get rid of that sneaky arrayifying spread operator...
It's always nice to avoid a second iteration
We also have space complexity
You're right, space is part of the equation too. It's all about what tradeoffs you're happy to make.
In this case, we're checking for duplicates in the string so we're either storing a memo hash (O(n) on space) or iterating over the pairs of elements (O(n^2) on time).
This one is O(n) on space and time, but you could defn make a fn that was O(1) on space if you were ok with O(n^2) on time.
(O(n) == O(2n) btw. The notation includes a multiplier to cover differences in the base case. So the top function up there where
[...s]
implicitly loops through the string before even hitting theevery
, actually has the same complexity as the lower function that is genuinely just one loop.)JavaScript
Efficient (for small strings)
works because if there are as many distinct characters as the length, there can't be duplicates
TextEncoder
TextEncoder but without sorting
TextEncoder without mutation
TextEncoder but finally a oneliner
Not too elegant, but a compact and efficient one-liner, using Python 3.8's assignment expressions.
Try it online!
Bitflags in Go
Javascript with less complexity
Solution in Haskell with
O(n)
time complexity:Here is the simple solution in Python:
JS one line