Pregunta de entrevista de Meta

Given a dictionary based simple password, create all possible (special character) passwords based on a provided mapping. Input: face Map: {a -> @, 4, A} Output: f@ce, f4ce, fAce

Respuestas de entrevistas

Anónimo

9 mar 2012

here's a javascript version using recursion: var mapPassword = (function () { var map = { a: ['A', '@', '4'], e: ['E', '#'] }; return function recur (pw) { //base case if (pw.length === 1) return map[pw] || pw; //otherwise var current = map[pw.charAt(0)], rest = recur(pw.slice(1)), //recursively get possible combinations for substring result = [], i, j, k, l; if (current) { // has map for (i = 0, j = current.length; i < j; i++) { for (k = 0, l = rest.length; k < l; k++) { result.push(current[i] + rest[k]); } } } else { // no map for (k = 0, l = rest.length; k < l; k++) { result.push(pw.charAt(0) + rest[k]); } } return result; } })();

1

Anónimo

12 nov 2012

Use Queue operation : #include #include #include #include #include typedef std::multimap CharMap; typedef CharMap::iterator mapIter; std::vector mapping(std::string input,int i, CharMap map_pass, mapIter m_Iter) { mapIter s_Iter; std::vector output; std::string outputStr = input; output.clear(); std::pair keyRange = map_pass.equal_range((*m_Iter).first); for(s_Iter = keyRange.first; s_Iter != keyRange.second; ++s_Iter) { outputStr[i] = (*s_Iter).second; output.push_back(outputStr); } return output; } void passwordmapping(std::string input, CharMap map_pass) { std::vector outVector; std::deque outputQueue; outputQueue.clear(); outVector.clear(); int inputlen = input.length(); mapIter m_Iter; outputQueue.push_back(input); int k=0; for(int i=0; i 0) { outVector = mapping(outputQueue.front(),i,map_pass,m_Iter); outputQueue.pop_front(); for(int j=0; j('a','@')); map_pass.insert(std::pair('a','A')); map_pass.insert(std::pair('a','4')); map_pass.insert(std::pair('e','E')); map_pass.insert(std::pair('e','#')); passwordmapping("face",map_pass); }

Anónimo

6 mar 2012

$map = array('a' => array('A', '@', '4')); function getAllSuggestions($input) { global $map; $toRet = array(); $len = count($input); $findCombs = ""; for($i=0; $i < $len; $i++) { if($map[$input[$i])) { $findCombs .= $input[$i]; } } $start = 0; $end = count($findCombs) - 1; $allCombs = getPossibleStrings($findCombs, $start, $end); } function getArr($in) { return $map[$in]; } function getPossibleStr($input, $statr, $end) { if ($input) { $len = $end - $start + 1; if ($len = 0) { return array(); } else if ($len == 1) { return getArr($input[$start]); } else { $toRet = array(); $first = getArr($input[$start]); $resPerms = getPossibleStr($input, $start + 1, $end); for ($i=0; $i