Skip to content
BytePatterns

Consistent Equalities

MediumUnion-Find#union-find#two-phase~25m

Problem

Each constraint is a four-character string over single lowercase letters, either "x==y" or "x!=y". Decide whether you can give every letter an integer value so that all the constraints hold at once.

Examples

Input:  rules = ["a==b", "b!=a"]
Output: False
Input:  rules = ["a==b", "b==c", "c!=d", "a!=d"]
Output: True
Why:    give a, b and c one value and d another
Input:  rules = ["a!=a"]
Output: False
Why:    edge case, a letter can never differ from itself

Hints

0 / 3

Stuck on the idea rather than the code? Path Compression covers it.