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. $item = rtrim($line, "\r\n");
  11. if ($item === '') {
  12. continue;
  13. }
  14. $list[] = $item;
  15. }
  16.  
  17. $result = [];
  18.  
  19. foreach ($list as $item) {
  20. $res = IsItsCharsAppearsTwice($item);
  21. if ($res) {
  22. $result[] = $res;
  23. }
  24. }
  25.  
  26. foreach ($result as $word) {
  27. echo $word, PHP_EOL;
  28. }
  29.  
  30. function IsItsCharsAppearsTwice($word)
  31. {
  32. $map = [];
  33. $len = strlen($word);
  34.  
  35. for ($i = 0; $i < $len; $i++) {
  36. $char = $word[$i];
  37. $map[$char] = ($map[$char] ?? 0) + 1;
  38. }
  39.  
  40. foreach ($map as $char => $count) {
  41. if ($count == 2) {
  42. return $word;
  43. }
  44. }
  45. return null;
  46. }
  47.  
  48.  
Success #stdin #stdout 0.03s 25824KB
stdin
asdf
fdas
asds
d fm
dfaa
aaaa
aabb
aaabb
stdout
asds
dfaa
aabb
aaabb