Water and Jug Problem

Solution Code
import collections

class Solution:
    def canMeasureWater(self, x: int, y: int, target: int) -> bool:
        if target > x + y:
            return False
        
        queue = collections.deque([0])
        visited = {0}
        steps = [x, -x, y, -y]
        
        while queue:
            curr = queue.popleft()
            if curr == target:
                return True
            for step in steps:
                nxt = curr + step
                if 0 <= nxt <= x + y and nxt not in visited:
                    visited.add(nxt)
                    queue.append(nxt)
        return False

Time Complexity

O(log(min(x, y))

Space Complexity

O(1)