Return the longest word with an even letter count and an odd digit count

Quick Overview

Find the longest word in a space-separated string whose letter count is even and whose digit count is odd, returning the first such word on a tie and an empty string when none qualifies. It tests string splitting, parity checks on character counts and careful tie-breaking.

Return the longest word with an even letter count and an odd digit count

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s` consisting of words separated by single spaces, where each word consists of alphanumeric characters, return the longest word that satisfies both of the following conditions: - The number of alphabetic characters in the word is even. - The number of digit characters in the word is odd. If no such word exists, return an empty string. If multiple valid words share the maximum length, return the first such word encountered. ### Function Signature ```python def longest_valid_password(s: str) -> str: ... ``` ### Rules - A word is a maximal run of non-space characters. Because `s` has no leading, trailing or repeated spaces, splitting on a single space gives exactly the words. - Letters are `a` to `z` and `A` to `Z`, and digits are `0` to `9`. Every character of a word is one or the other. - Zero counts as even. A word with no letters is valid when its digit count is odd, and a word with no digits is never valid. - Length means the number of characters in the word. Among the valid words of greatest length, return the one that appears first in `s`. - Return `""` when no word is valid. ### Constraints - `1 <= len(s) <= 10^4` - `s` consists of alphanumeric characters and single spaces. - `s` does not start or end with a space and does not contain consecutive spaces. ### Examples ```text Input: s = "abc12 ab3 hello1 ab12c3" Output: "ab3" ``` "abc12", "hello1" and "ab12c3" have 3, 5 and 3 letters, all odd counts, so they fail. "ab3" has 2 letters and 1 digit, which makes it the only valid word. ```text Input: s = "a1 bb22 cc1 dd333" Output: "dd333" ``` "a1" has an odd letter count and "bb22" has an even digit count, so both fail. "cc1" (length 3) and "dd333" (length 5) are both valid, and "dd333" is longer. ```text Input: s = "x1y 9 ab5 q" Output: "x1y" ``` "x1y", "9" and "ab5" are valid ("9" has zero letters, which is even), while "q" has one letter. "x1y" and "ab5" tie at length 3, and "x1y" comes first.

Overview: Find the longest word in a space-separated string whose letter count is even and whose digit count is odd, returning the first such word on a tie and an empty string when none qualifies. It tests string splitting, parity checks on character counts and careful tie-breaking.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s consisting of words separated by single spaces, where each word consists of alphanumeric characters, return the longest word that satisfies both of the following conditions:

  • The number of alphabetic characters in the word is even.
  • The number of digit characters in the word is odd.

If no such word exists, return an empty string. If multiple valid words share the maximum length, return the first such word encountered.

Function Signature

def longest_valid_password(s: str) -> str:
    ...

Rules

  • A word is a maximal run of non-space characters. Because s has no leading, trailing or repeated spaces, splitting on a single space gives exactly the words.
  • Letters are a to z and A to Z , and digits are 0 to 9 . Every character of a word is one or the other.
  • Zero counts as even. A word with no letters is valid when its digit count is odd, and a word with no digits is never valid.
  • Length means the number of characters in the word. Among the valid words of greatest length, return the one that appears first in s .
  • Return "" when no word is valid.

Constraints

  • 1 <= len(s) <= 10^4
  • s consists of alphanumeric characters and single spaces.
  • s does not start or end with a space and does not contain consecutive spaces.

Examples

Input: s = "abc12 ab3 hello1 ab12c3"
Output: "ab3"

"abc12", "hello1" and "ab12c3" have 3, 5 and 3 letters, all odd counts, so they fail. "ab3" has 2 letters and 1 digit, which makes it the only valid word.

Input: s = "a1 bb22 cc1 dd333"
Output: "dd333"

"a1" has an odd letter count and "bb22" has an even digit count, so both fail. "cc1" (length 3) and "dd333" (length 5) are both valid, and "dd333" is longer.

Input: s = "x1y 9 ab5 q"
Output: "x1y"

"x1y", "9" and "ab5" are valid ("9" has zero letters, which is even), while "q" has one letter. "x1y" and "ab5" tie at length 3, and "x1y" comes first.