You are writing a password validator for a website. You want to discourage users from using their username as part of their password, or vice-versa, because it is insecure. Since its unreasonable to not allow them to have any letters in common, you come up with this rule:
For any password and username pair, the length of the longest common substring should be less than half the length of the shortest of the two.
In other words, you won't allow users to have half their password repeated in their username, or half their username repeated in their password.
Write a function validate(username, password)
which returns true if your rule is followed, false otherwise.
Assume both the username and the password may contain uppercase letters, lowercase letters, numbers, spaces, and the following special/punctation characters: !@#$%^&*()_+{}:"<>?[];',. The username and password will each be less than 100 characters.
Examples
validate("newyorkcity", "yankees101"), TRUE
validate("username", "usernamePW"), FALSE
Tests
validate("Paa2", "Paaaaaa222!!!")
validate("wordpass", "password")
validate("superman", "persuzod")
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (3)
Here is the simple solution with Python and
difflib
module:Here's a solution in Dyalog APL:
Running it:
But don't take my word for it - Try It Online!
Rust
Look at it go!
A silly one.
Regex with range subtraction (all characters
'!'
to'z'
are accepted, except-
/
=
and\
), also checks for length.So that regex is compiled only once, we construct it lazily.
Then we pick the shorter of the two strings, get all the overlapping windows into it, and colleсt them into a
HashSet
for uniqueness.Then we use an Aho-Corasick automaton with all of these substrings to match on them simultaneously in one pass.
However, if you think about it, the same thing can be written without dependencies/nightly features
This trades CPU for memory, for instance:
Look at it go!