DEV Community

dev.to staff
dev.to staff

Posted on

6 1

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!

AWS Q Developer image

What is MCP? No, Really!

See MCP in action and explore how MCP decouples agents from servers, allowing for seamless integration with cloud-based resources and remote functionality.

Watch the demo

Top comments (6)

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli • 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-Tsivintzeli • Edited

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

Thread Thread
 
qm3ster profile image
Mihail Malo

RIP

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
Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli • 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

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server ⏰

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

If this **helped, please leave a ❤️ or a friendly comment!

Okay