xoxama

Longest Block in String

Apr 23rd, 2020
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Longest Block in String
  2.  
  3. const input = ['abbb'];
  4. const print = this.print || console.log;
  5. const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  6.  
  7. const strA = gets().split(''); // print(strA) // [ 'h', 'o', 'o', 'p', 'a' ]
  8. let arrN = [];
  9. let countMax = 1;
  10. let count = 1;
  11.  
  12. if (strA.length >= 2) {
  13.   for (let i = 0; i < strA.length - 1; i++) {
  14.     if (strA[i] === strA[i + 1]) {
  15.       count++;
  16.       if (count > countMax) {
  17.         countMax = count;
  18.         arrN = [];
  19.         for (let j = 0; j < countMax; j++) {
  20.           arrN.push(strA[i]);
  21.         }
  22.       }
  23.     } else {
  24.       count = 1;
  25.     }
  26.   }
  27. }
  28.  
  29. print(arrN.join(''));
Advertisement
Add Comment
Please, Sign In to add comment