Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Check Whether Digits Alternate in Parity

Basic Acceptance 78.04% Points 10.00

You are given a non-negative integer n. Your task is to determine whether its digits alternate in parity when read from left to right.

The digits must alternate between odd and even, or even and odd, throughout the number. Two consecutive digits must never have the same parity. Return Yes if the digits alternate in parity; otherwise, return No.

Examples
Example 1

Input: n = 1234
Output: Yes
Explanation: The digits have alternating parity: 1 (odd), 2 (even), 3 (odd), and 4 (even).

Example 2

Input: n = 1245
Output: No
Explanation: The digits 2 and 4 are both even, so the parity alternation is broken.

Example 3

Input: n = 8
Output: Yes
Explanation: A single-digit number satisfies the condition because there are no consecutive digits to compare.

Constraints
  • 0 = n = 10¹8
Companies
Samsung Cisco
Topics
Data Structures Basics String Searching Algorithms
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions