Last active
May 9, 2018 12:02
-
-
Save abhinavnigam2207/5dae20efa0869bef5a921122792e8cc5 to your computer and use it in GitHub Desktop.
String matching time complexity problem (Asked in Castle Global)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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