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. return Object.values(
  9. [...line].reduce(
  10. (acc, char) => ({ ...acc, [char]: (acc[char] || 0) + 1 }),
  11. {}
  12. )
  13. ).some((count) => count === charCount);
  14. }
  15.  
  16. // Read input line by line using `readline` function
  17. // Check if each line has any character appearing exactly twice, and print it if so
  18. while ((line = readline())) {
  19. if (hasExactChars(line, 2)) {
  20. print(line);
  21. }
  22. }
  23.  
Success #stdin #stdout 0.04s 17556KB
stdin
asdf
fdas
asds
d fm
dfaa
aaaa
aabb
aaabb
stdout
asds
dfaa
aabb
aaabb