fork download
  1. /**
  2.  * Check if a string contains any character appearing exactly `count` times
  3.  * @param {string} line - The input string to be checked
  4.  * @param {number} [charCount=2] - The required number of occurrences for a character to be considered
  5.  * @returns {boolean} - True if any character appears exactly `charCount` times, false otherwise
  6.  */
  7. function hasExactChars(line = '', charCount = 2) {
  8. // Count the occurrences of each character in the line
  9. const charCounts = [...line].reduce(
  10. (acc, char) => ({ ...acc, [char]: (acc[char] || 0) + 1 }),
  11. {}
  12. );
  13.  
  14. // Check if any character has the required count of occurrences
  15. return Object.values(charCounts).some((count) => count === charCount);
  16. };
  17.  
  18. // Read input line by line using `readline` function
  19. // Check if each line has any character appearing exactly twice, and print it if so
  20. while (line = readline()) {
  21. if (hasExactChars(line, 2)) {
  22. print(line);
  23. }
  24. }
  25.  
Success #stdin #stdout 0.03s 17788KB
stdin
asdf
fdas
asds
d fm
dfaa
aaaa
aabb
aaabb
stdout
asds
dfaa
aabb
aaabb