The password database seems to be corrupted: some of the passwords wouldn't have been allowed by the required corporate password policy.  that was in effect when they were chosen. For example, suppose you have the following list:

1-3 a: aaddbbdd

1-4 b: aassddf

1-8 c: cccccccc

Each line gives the password policy and then the password. The password policy indicates the lowest and highest number of times a given letter must appear for the password to be valid. For example, 1-3 a means that the password must contain a at least 1 time and at most 3 times. In the above example, 2 passwords are valid. 

How many passwords are valid according to their policies?


if this is a java coding interview and you see list of data it means interview expects to see java 8 solution. This is first sign.

Try to ask right questions first:

- do we have each string in format we have?

- Should we validate String before?

- ....

As you see this is java 8 problem with pricing of data, validation and count of result. It's better to present you knowledge of Ja=va 8 patterns, terminology, group working with regular expression.

private static Optional<Tuple<Predicate, String>> parseLine(String condition) {
    Pattern pattern = Pattern.compile("^([1-9][0-9]*)-([1-9][0-9]*) ([a-zA-Z]): ([a-zA-Z]+)$");
    Matcher matcher = pattern.matcher(condition);
    if(matcher.find() && matcher.groupCount() >= 4) {
        String lowEnd      =;
        String upperEnd    =;
        String letter      =;
        String password    =;
        String regex       = "[" + letter +"]{" + lowEnd +  "," + upperEnd + "}";
        String notMyLetter = "[^" + letter + "]";
        return Optional.of(new Tuple<>(s -> s.trim().replaceAll(notMyLetter, "").matches(regex),
    else return Optional.empty();
private static final Predicate notNull     = Objects::nonNull;
private static final Predicate notEmpty    = s -> s.trim().length() > 0;
private static final Predicate validString = notNull.and(notEmpty);
private static final Function<List, Stream<Tuple<Predicate, String>>> parse =
  l ->
private static final Function<Stream<Tuple<Predicate, String>>, Stream> validate =
    rs -> rs.filter(r -> r._1().test(r._2()))
private static final Function<List, Long> solution = parse.andThen(validate).andThen(Stream::count);