Created
March 16, 2021 21:57
-
-
Save MohammedALREAI/9fbc54754aee2492acffebd2da2dc011 to your computer and use it in GitHub Desktop.
areFollowingPatterns in the codesignal
This file contains hidden or 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
function areFollowingPatterns(strings: string[], patterns: string[]): boolean { | |
for (let i: number = 0; i < strings.length; i++) { | |
if (patterns.indexOf(patterns[i]) !== strings.indexOf(strings[i])) return false; | |
} | |
return true; | |
} | |
or | |
function areFollowingPatterns(strings: string[], patterns: string[]): boolean { | |
let hashStrings= {}; | |
let hashPatterns= {}; | |
for(let i=0;i<strings.length;i++){ | |
if(hashStrings[strings[i]]===undefined){ | |
hashStrings[strings[i]]=patterns[i]; | |
} | |
else{ | |
if(patterns[i]!==hashStrings[strings[i]]) return false; | |
} | |
if(hashPatterns[patterns[i]]===undefined){ | |
hashPatterns[patterns[i]]=strings[i]; | |
} | |
else{ | |
if(strings[i]!==hashPatterns[patterns[i]]) return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment