Hacker News new | ask | show | jobs
by comsolo 5372 days ago
err 20 min? It's about a 60 second problem.

<?php $lines = explode("\n", strtolower(file_get_contents("input.txt"))); foreach ($lines as $ln) { for ($i = 0; $i < strlen($ln); $i++) { $char = $ln[$i]; $first_pos = strpos($ln, $char); $second_pos = strpos($ln, $char, $first_pos+1); $is_repeating = $second_pos !== false; if (!$is_repeating) { echo $char."\n"; break; } } }

?>

2 comments

Also, this solution isn't the most efficient because again you're searching the whole string potentially every iteration of your inner loop. So you've got an O(n^2) algorithm when it could be an O(n) algorithm. If we're dealing with just a short string, fine, but if the string were tens of thousands of characters long and we had thousands of lines, this might star to become significant. Sure, we're not likely to be given that. Maybe I read too much into these problems, but if they're going to give this as a test I assume they actually want to test something like efficiency, readability, etc.
You'd lose points from me here for using $ln instead of $line. One point because you're just shaving off two letters at the cost of making me go "huh, whats that... oh". And then another because it's inconsistent. If you're going to use $ln use $lns for the plural, or if you use $lines use $line for singular. Don't you think? Of course I know this was a quick example but that's the kind of stuff that I think about.