Skip to content

Instantly share code, notes, and snippets.

@amahdy
Created August 23, 2022 18:06
Show Gist options
  • Save amahdy/19f9d784ddda87d30adf8bc82ebd14a2 to your computer and use it in GitHub Desktop.
Save amahdy/19f9d784ddda87d30adf8bc82ebd14a2 to your computer and use it in GitHub Desktop.
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let maxLen = -1;
let currentLen = 0;
let charCount = 0;
let numbCount = 0;
let err=0;
for(let i=0; i<=S.length; i++) {
if(i==S.length || S[i]==' ') {
if(err==0
&& (numbCount&1 || numbCount==0)
&& (!(charCount&1) || charCount==0)
&& currentLen>maxLen) {
maxLen=currentLen;
}
err=0;
numbCount=0;
charCount=0;
currentLen=0;
continue;
} else if (/^[a-zA-Z]+$/i.test(S[i])) {
charCount++;
} else if (/^[0-9]+$/i.test(S[i])) {
numbCount++;
} else {
err=1;
}
currentLen++;
// console.log(numbCount, charCount, currentLen, err, maxLen);
}
return maxLen;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment