Skip to content

Instantly share code, notes, and snippets.

@abhinavnigam2207
Last active May 9, 2018 12:02
Show Gist options
  • Save abhinavnigam2207/5dae20efa0869bef5a921122792e8cc5 to your computer and use it in GitHub Desktop.
Save abhinavnigam2207/5dae20efa0869bef5a921122792e8cc5 to your computer and use it in GitHub Desktop.
String matching time complexity problem (Asked in Castle Global)
const problemSolution = function(str){
let val = str.split(''),
stack = [],
count = 0;
stack.push(val[0]);
for(let i=1; i< val.length; i++) {
if((/[a-z]/.test(stack[stack.length-1])) && (/[a-z]/.test(val[i]))) {
console.log('1'+ val[i]);
return count;
}
else if((/[A-Z]/.test(stack[stack.length-1])) && (/[A-Z]/.test(val[i]))) {
console.log('2'+ val[i]);
stack.push(val[i]);
++count;
}
else if(stack[stack.length-1] && (/[a-z]/.test(val[i])) && stack[stack.length-1].toLowerCase() === val[i]) {
console.log('3'+val[i]);
++count;
stack.pop();
}
else {
console.log('4'+ val[i]);
stack.push(val[i]);
}
}
return count;
}
@abhinavnigam2207
Copy link
Author

abhinavnigam2207 commented May 8, 2018

String Matching problem with s set of rules
i). Count 1 if an Upper case letter is followed by any Upper case letter

ii). Count 1 if an Upper case letter is followed by its same Lower case letter and dissolve the pair. (After dissolving again check condition 2 if it needs to be dissolved again)

iii). if two lower case letters are present without/with a match stop and return count

Example:

ABba
count= 3

gGdLLgCDBbdcf
count= 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment