DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #297 - Loneliest Character

Your task is to write a function loneliest() which accepts a string and will return the character that has the most spaces to its right and left.

Examples

'a b c' => ['b']
'a bcs d k' => ['d']
' a b sc p t k' => ['p']
'a b c de' => ['b', 'c']
' a b c de ' => ['b']
'abc' => ['a', 'b', 'c']

Tests

loneliest('abc d ef g h i j ')
loneliest('abc')
loneliest(' abc d z f gk s ')

Good luck!


This challenge comes from aplefull on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (6)

Collapse
 
mellen profile image
Matt Ellen • Edited

Only the last example make sense.

There has been some corruption and information removal. The actual examples are as follows:

                     'a b  c' => ['b']
        'a bcs           d k' => ['d']
'    a b  sc     p     t   k' => ['p']
                'a  b  c  de' => ['b', 'c']
    '     a  b  c de        ' => ['b']
                        'abc' => ['a', 'b', 'c']
Enter fullscreen mode Exit fullscreen mode

Also there is the important note as follows:

String can have leading/trailing spaces, you should not count them;

Strings contain only unique characters from a to z;

Order of characters in array doesn't matter;

Collapse
 
qm3ster profile image
Mihail Malo

So first and last non-whitespace character are less likely to be lonely, because they don't have space on one side?
For example, why is the first answer not ['b', 'c']? 'c' is as far from the other letters as it gets!

Collapse
 
mellen profile image
Matt Ellen • Edited

I didn't write the rules, just copied them from the other site :-Þ

Thread Thread
 
qm3ster profile image
Mihail Malo

RIP

Collapse
 
mellen profile image
Matt Ellen • Edited
function lonliest(str)
{
  str = str.trim();
  let spaces = new Map();
  let curLetter = str[0];
  let spaceCount = 0;
  let biggestCount = 0;
  for(c of str)
  {
    if(c == ' ')
    {
      spaceCount++;
    }
    else
    {
      let sc = (spaces.has(curLetter)?spaces.get(curLetter):0)+spaceCount;
      spaces.set(curLetter, sc);
      if(sc > biggestCount)
      {
          biggestCount = sc;
      }
      curLetter = c;
      spaces.set(curLetter, spaceCount);
      spaceCount = 0;
    }
  }

  return [...spaces].filter(sc => sc[1] == biggestCount).map(sc => sc[0]);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
_bkeren profile image
''

JS


const loneliest = string => {
  let letterSpaceStatus= []
  for (let i = 0; i < string.length;) {
    if (string.charAt(i) !== ' ') {
      let letterIndex = i
      let rightSpaceCount = 0,
        spaceCount = 0
      if (letterSpaceStatus.length > 0) {
        spaceCount = letterSpaceStatus[letterSpaceStatus.length - 1].rightSpaceCount;
      }
      i++
      while (i < string.length && string.charAt(i) === ' ') {
        i++
        spaceCount++
        rightSpaceCount++
      }
      letterSpaceStatus[letterSpaceStatus.length] = {
        letter: string.charAt(letterIndex),
        spaceCount,
        rightSpaceCount
      }
    } else {
    i++
    }
  }

  const maxSpaceCount = Math.max(...letterSpaceStatus.map(li => li.spaceCount))
  return letterSpaceStatus.filter(i => i.spaceCount === maxSpaceCount).map(i => i.letter)
}

Enter fullscreen mode Exit fullscreen mode