Created
February 7, 2017 05:54
-
-
Save azat-co/05810e893b872182144bcba793ec85f6 to your computer and use it in GitHub Desktop.
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 assert = require('assert') | |
const solution = (str) => { | |
const checkSubs = (s, oldResults=[]) => { | |
let start = s.indexOf('{') | |
let end = s.indexOf('}', start+1) | |
// console.log('yo', s, start, end,s.substring(end+1, s.length)) | |
if (start<0 && end <0) { | |
for (let i = 0; i<oldResults.length; i++) { | |
oldResults[i] += s | |
} | |
return oldResults | |
} else { | |
let pattern = s.substring(start+1, end).split(',') | |
let results = [] | |
for (let i=0; i<pattern.length; i++) { | |
if (oldResults.length == 0) results.push(s.substring(0, start)+pattern[i]) | |
else for (let j=0; j<oldResults.length; j++) { | |
results.push(oldResults[j] + s.substring(0, start)+pattern[i]) | |
} | |
} | |
// console.log(pattern, results) | |
return checkSubs(s.substring(end+1, s.length), results) | |
} | |
} | |
return checkSubs(str, []) | |
} | |
assert.deepEqual(['hello_1_A', 'hello_2_A', 'hello_3_A', 'hello_1_B', 'hello_2_B', 'hello_3_B'], solution('hello_{1,2,3}_{A,B}')) | |
assert.deepEqual(['1@m!', '2@m!', '1@l!', '2@l!', '1@m!', '2@m!'], solution('{1,2}@{m,l,m}!')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment