Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const fs = require('fs');
  4.  
  5. process.stdin.resume();
  6. process.stdin.setEncoding('utf-8');
  7.  
  8. let inputString = '';
  9. let currentLine = 0;
  10.  
  11. process.stdin.on('data', inputStdin => {
  12. inputString += inputStdin;
  13. });
  14.  
  15. process.stdin.on('end', _ => {
  16. inputString = inputString.replace(/\s*$/, '')
  17. .split('\n')
  18. .map(str => str.replace(/\s*$/, ''));
  19.  
  20. main();
  21. });
  22.  
  23. function readLine() {
  24. return inputString[currentLine++];
  25. }
  26.  
  27. // Solution start
  28.  
  29. function getColorFreq(bugs) { // :Object{color:count, ...}
  30. return bugs.split("").reduce(
  31. (a, b) => {
  32. a[b] = (a[b] | 0) + 1;
  33. return a;
  34. }, {}
  35. );
  36. }
  37.  
  38. function onlySpaces(bugs) { // :Boolean
  39. return bugs.match(/^_+$/g) != null;
  40. }
  41.  
  42. function alreadyHappy(bugs) { // :Boolean
  43. if (onlySpaces(bugs)) return true;
  44.  
  45. const len = bugs.length;
  46. if (len < 2 ||
  47. bugs[0] !== bugs[1] ||
  48. bugs[len - 1] !== bugs[len - 2]) return false;
  49.  
  50. for (let i = 1; i < len - 2; ++i) {
  51. if (
  52. bugs[i] !== "_" &&
  53. bugs[i] !== bugs[i - 1] &&
  54. bugs[i] !== bugs[i + 1]
  55. ) return false;
  56. }
  57. return true;
  58. }
  59.  
  60. function canBeHappy(bugs) { // :Boolean
  61. const freq = getColorFreq(bugs);
  62. for (const color in freq) {
  63. if (color !== '_' && freq[color] < 2)
  64. return false;
  65. }
  66. return freq['_'] > 0;
  67. }
  68.  
  69. // Complete the happyLadybugs function below.
  70. function happyLadybugs(bugs) {
  71. return alreadyHappy(bugs) || canBeHappy(bugs) ? "YES" : "NO";
  72. }
  73.  
  74. function tests() {
  75.  
  76. console.assert(onlySpaces("_") === true);
  77. console.assert(onlySpaces("_____") === true);
  78. console.assert(onlySpaces("_____") === true);
  79. console.assert(onlySpaces("A____") === false);
  80. console.assert(onlySpaces("__A__") === false);
  81. console.assert(onlySpaces("____A") === false);
  82.  
  83. const freq = getColorFreq("ABCD_BCD_CD_D");
  84. console.assert(freq['A'] === 1);
  85. console.assert(freq['B'] === 2);
  86. console.assert(freq['C'] === 3);
  87. console.assert(freq['D'] === 4);
  88. console.assert(freq['_'] === 3);
  89.  
  90. console.assert(alreadyHappy("BBCCAA") === true);
  91. console.assert(alreadyHappy("BB") === true);
  92. console.assert(alreadyHappy("BBCC_AA") === true);
  93. console.assert(alreadyHappy("A") === false);
  94. console.assert(alreadyHappy("BBC_CAA") === false);
  95. console.assert(alreadyHappy("BCCAAB") === false);
  96. console.assert(alreadyHappy("BCC_AAB") === false);
  97. }
  98.  
  99. function main() {
  100.  
  101. tests();
  102.  
  103. const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
  104.  
  105. const g = parseInt(readLine(), 10);
  106.  
  107. for (let gItr = 0; gItr < g; gItr++) {
  108. const n = parseInt(readLine(), 10);
  109.  
  110. const b = readLine();
  111.  
  112. let result = happyLadybugs(b);
  113.  
  114. ws.write(result + "\n");
  115. }
  116.  
  117. ws.end();
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement