Skip to the code
cf.
+10 XP

#479 Draw the Icon in One Stroke

easy

An icon editor has a "single-stroke" check for line icons. The icon has points numbered 0 to n - 1, and lines[i] = [a, b] is a straight line between points a and b.

Write oneStroke(n, lines) that returns true if the icon can be drawn without lifting the pen and without drawing any line twice, and false otherwise. Points with no lines don't matter. An icon with no lines counts as drawable.

Example 1

Input: n = 4, lines = [[0,1],[1,2],[2,3],[3,0],[0,2]]
Output: true

A square with one diagonal: points 0 and 2 touch 3 lines each and the others touch 2, so start at 0 and finish at 2.

Example 2

Input: n = 4, lines = [[0,1],[0,2],[0,3]]
Output: false

Four points touch an odd number of lines. A single stroke allows at most two.

Constraints

  • 1 <= n <= 10000
  • 0 <= lines.length <= 50000
  • 0 <= a, b < n, a !== b
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).