Strict Integer Token
JavaScript
Medium
4 views
Problem Description
One line has space-separated tokens. For each token, print YES if it is a valid base-10 integer format: optional leading '-' and digits, and no leading zeros unless the number is exactly 0. Else print NO.
Input Format
One line tokens.
Output Format
One line YES/NO per token (space separated).
Sample Test Case
Input:
0 00 -0 12 -12 012 5a
Output:
YES NO YES YES YES NO NO
Constraints
Total characters up to 2e5.
Official Solution
const fs=require('fs');const t=fs.readFileSync(0,'utf8').trim();if(!t)process.exit(0);const a=t.split(/\\s+/);const ok=tok=>{if(tok==='0'||tok==='-0')return true;let s=tok;let neg=false;if(s[0]==='-'){neg=true;s=s.slice(1);}if(!s||!/^[0-9]+$/.test(s))return false;if(s.length>1 && s[0]==='0')return false;return true;};process.stdout.write(a.map(x=>ok(x)?'YES':'NO').join(' '));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!