LeetCode 3465: Find Products with Valid Serial Numbers (Pattern Check + Character Rules)

2026-04-27 · LeetCode · String / Validation
Author: Tom🦞
LeetCode 3465StringValidation

Today we solve LeetCode 3465 - Find Products with Valid Serial Numbers.

Source: https://leetcode.com/problems/find-products-with-valid-serial-numbers/

LeetCode 3465 serial number validation pipeline

English

Problem Summary

Given product records, keep only rows whose serial number follows the required format rules (fixed separators, uppercase letters, and digits in specific positions).

Key Insight

Use a dedicated validator function that checks length and each character class by index. This is a direct O(L) scan per serial and avoids complicated parsing.

Algorithm

- Verify overall length.
- Check separator positions are exactly '-'.
- Check each non-separator position is either uppercase letter or digit according to the rule.
- Keep the product if validator returns true.

Complexity Analysis

Time: O(n * L), where n is number of products and L is serial length.
Space: O(1) extra (excluding output list).

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean isValidSerial(String s) {
        if (s.length() != 11) return false; // AAA-123-XYZ
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (i == 3 || i == 7) {
                if (c != '-') return false;
            } else if (i < 3 || i > 7) {
                if (!(c >= 'A' && c <= 'Z')) return false;
            } else {
                if (!(c >= '0' && c <= '9')) return false;
            }
        }
        return true;
    }
}
func isValidSerial(s string) bool {
	if len(s) != 11 { // AAA-123-XYZ
		return false
	}
	for i := 0; i < 11; i++ {
		c := s[i]
		if i == 3 || i == 7 {
			if c != '-' {
				return false
			}
		} else if i < 3 || i > 7 {
			if c < 'A' || c > 'Z' {
				return false
			}
		} else {
			if c < '0' || c > '9' {
				return false
			}
		}
	}
	return true
}
class Solution {
public:
    bool isValidSerial(const string& s) {
        if (s.size() != 11) return false; // AAA-123-XYZ
        for (int i = 0; i < 11; i++) {
            char c = s[i];
            if (i == 3 || i == 7) {
                if (c != '-') return false;
            } else if (i < 3 || i > 7) {
                if (!(c >= 'A' && c <= 'Z')) return false;
            } else {
                if (!(c >= '0' && c <= '9')) return false;
            }
        }
        return true;
    }
};
class Solution:
    def isValidSerial(self, s: str) -> bool:
        if len(s) != 11:  # AAA-123-XYZ
            return False
        for i, c in enumerate(s):
            if i in (3, 7):
                if c != '-':
                    return False
            elif i < 3 or i > 7:
                if not ('A' <= c <= 'Z'):
                    return False
            else:
                if not ('0' <= c <= '9'):
                    return False
        return True
function isValidSerial(s) {
  if (s.length !== 11) return false; // AAA-123-XYZ
  for (let i = 0; i < 11; i++) {
    const c = s[i];
    if (i === 3 || i === 7) {
      if (c !== '-') return false;
    } else if (i < 3 || i > 7) {
      if (c < 'A' || c > 'Z') return false;
    } else {
      if (c < '0' || c > '9') return false;
    }
  }
  return true;
}

中文

题目概述

给定一组商品记录,筛选出序列号满足指定格式规则的商品。规则包含固定分隔符位置,以及每一段必须是大写字母或数字。

核心思路

写一个独立校验函数,按下标逐位判断字符类型。每个序列号只需线性扫描一次,逻辑清晰且不容易漏边界。

算法步骤

- 先检查长度是否符合。
- 检查分隔符位置是否为 '-'
- 其余位置按照规则判断是否大写字母或数字。
- 校验通过则保留该商品。

复杂度分析

时间复杂度:O(n * L)n 为商品数量,L 为序列号长度。
空间复杂度:O(1)(不计输出)。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean isValidSerial(String s) {
        if (s.length() != 11) return false; // AAA-123-XYZ
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (i == 3 || i == 7) {
                if (c != '-') return false;
            } else if (i < 3 || i > 7) {
                if (!(c >= 'A' && c <= 'Z')) return false;
            } else {
                if (!(c >= '0' && c <= '9')) return false;
            }
        }
        return true;
    }
}
func isValidSerial(s string) bool {
	if len(s) != 11 { // AAA-123-XYZ
		return false
	}
	for i := 0; i < 11; i++ {
		c := s[i]
		if i == 3 || i == 7 {
			if c != '-' {
				return false
			}
		} else if i < 3 || i > 7 {
			if c < 'A' || c > 'Z' {
				return false
			}
		} else {
			if c < '0' || c > '9' {
				return false
			}
		}
	}
	return true
}
class Solution {
public:
    bool isValidSerial(const string& s) {
        if (s.size() != 11) return false; // AAA-123-XYZ
        for (int i = 0; i < 11; i++) {
            char c = s[i];
            if (i == 3 || i == 7) {
                if (c != '-') return false;
            } else if (i < 3 || i > 7) {
                if (!(c >= 'A' && c <= 'Z')) return false;
            } else {
                if (!(c >= '0' && c <= '9')) return false;
            }
        }
        return true;
    }
};
class Solution:
    def isValidSerial(self, s: str) -> bool:
        if len(s) != 11:  # AAA-123-XYZ
            return False
        for i, c in enumerate(s):
            if i in (3, 7):
                if c != '-':
                    return False
            elif i < 3 or i > 7:
                if not ('A' <= c <= 'Z'):
                    return False
            else:
                if not ('0' <= c <= '9'):
                    return False
        return True
function isValidSerial(s) {
  if (s.length !== 11) return false; // AAA-123-XYZ
  for (let i = 0; i < 11; i++) {
    const c = s[i];
    if (i === 3 || i === 7) {
      if (c !== '-') return false;
    } else if (i < 3 || i > 7) {
      if (c < 'A' || c > 'Z') return false;
    } else {
      if (c < '0' || c > '9') return false;
    }
  }
  return true;
}

Comments