Yesterday night i came across this puzzle posted by facebook for job interview and here’s how i tried to solve it i couldn’t test the solution as the timer timed out and i could not find to verify my answer on interview street for the puzzle after the timeout so i posted it on my site hoping someone might help me to verify wether the solution is correct or incorrect.
Problem:
String s is called unique if all the characters of s are different.
String s2 is producible from string s1, if we can remove some characters of s1 to obtain s2.
String s1 is more beautiful than string s2 if length of s1 is more than length of s2 or they have equal length and s1 is lexicographically greater than s2.
Given a string s you have to find the most beautiful unique string that is producible from s.
Input:
First line of input comes a string s having no more than 1,000,000(10^6) characters. all the characters of s are lowercase english letters.
Output:
Print the most beautiful unique string that is producable from s
Sample Input:
babab
Sample Output:
ba
Explanation
In the above test case all unique strings that are producible from s are “ab” and “ba” and “ba” is more beautiful than “ab”.
Solution:
<?php
function fb()
{
$file = fopen('php://stdin', "r" ) or die("Couldn't open $filename");
while ( ! feof( $file ) ) {
$line = fgets( $file);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
$s = $line;
$unique = implode("",array_unique(str_split($s)));
$sn = permute($unique,0,strlen($unique));
// Convert array to an array of string lengths
$max_len = max(array_map('strlen', $sn));
$s1 = array();
foreach($sn as $key => $val) {
$val_len = strlen($val);
if ( $val_len = $max_len ){
$s1[] = $val;
}
}
sort($s1);
$sf1 = array_reverse($s1,false);
fwrite(STDOUT, $sf1['0']);
}
}
function permute($str) {
/* If we only have a single character, return it */
if (strlen($str) < 2) {
return array($str);
}
/* Initialize the return value */
$permutations = array();
/* Copy the string except for the first character */
$tail = substr($str, 1);
/* Loop through the permutations of the substring created above */
foreach (permute($tail) as $permutation) {
/* Get the length of the current permutation */
$length = strlen($permutation);
/* Loop through the permutation and insert the first character of the original
string between the two parts and store it in the result array */
for ($i = 0; $i <= $length; $i++) {
$permutations[] = substr($permutation, 0, $i) . $str[0] . substr($permutation, $i);
}
}
/* Return the result */
return $permutations;
}
fb();
?>
You could try the this script out with the command line:
Apple doesn’t sell iPhone’s to its customers because of its hardware specs they sell the experience of using it.
iPhone is a standard. Other phone manufacturers compare against the iPhone.
The iPhone feels really good in your hand.
Every interaction with iOS just feels right.
Great variety of applications because it had a first mover advantage and its here to stay because developers want to build apps first for high end users.
Siri is by no means perfect and cannot be used in day to day life unless you have a lot of time to waste but by far its the best artificial personal assistant i have seen yet and it will only improve with time.