fork(4) download
  1. <?php
  2.  
  3. if (php_sapi_name() !== 'cli') {
  4. fwrite(STDERR, "Run this script from the terminal.\n");
  5. exit(1);
  6. }
  7.  
  8. $list = [];
  9. while (($line = fgets(STDIN)) !== false) {
  10. $list[] = rtrim($line, "\r\n");
  11. }
  12.  
  13. $result = [];
  14.  
  15. foreach ($list as $item) {
  16. $res = IsItsCharsAppearsTwice($item);
  17. if ($res) {
  18. $result[] = $res;
  19. }
  20. }
  21.  
  22. foreach ($result as $word) {
  23. echo $word, PHP_EOL;
  24. }
  25.  
  26. function IsItsCharsAppearsTwice($word)
  27. {
  28. $map = [];
  29. $len = strlen($word);
  30.  
  31. for ($i = 0; $i < $len; $i++) {
  32. $char = $word[$i];
  33. $map[$char] = ($map[$char] ?? 0) + 1;
  34. }
  35.  
  36. foreach ($map as $char => $count) {
  37. if ($count == 2) {
  38. return $word;
  39. }
  40. }
  41. return null;
  42. }
Success #stdin #stdout 0.03s 25372KB
stdin
asdf
fdas
asds
d fm
dfaa
aaaa
aabb
aaabb
stdout
asds
dfaa
aabb
aaabb