DEV Community

Cover image for CamelCase Hackerrank [Easy Solution]
Yajindra Gautam
Yajindra Gautam

Posted on

5

CamelCase Hackerrank [Easy Solution]

Problem Description

There is a sequence of words in CamelCase as a string of letters, , having the following properties:

  • It is a concatenation of one or more words consisting of English letters.
  • All letters in the first word are lowercase.
  • For each of the subsequent words, the first letter is uppercase and rest of the letters are lowercase. Given , determine the number of words in .

Example
s = "ontTwoThree"

There are words in the string: 'one', 'Two', 'Three'.

Function Description

Complete the camelcase function in the editor below.

camelcase has the following parameter(s):

  • string s: the string to analyze

Returns

  • int: the number of words in s

Input Format

A single line containing string s .

Sample Input

saveChangesInTheEditor
Enter fullscreen mode Exit fullscreen mode

Sample Output

5
Enter fullscreen mode Exit fullscreen mode

Explanation

String contains five words:

  1. save
  2. Change
  3. In
  4. The
  5. Editor

My Solution

function camelcase(s) {
    // Write your code here
     const strArr = s.split(/(?=[A-Z])/)
    return  strArr.length ;
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation

Firstly, I have split the given string not only that I have split string using REGEX /(?=[A-Z])/. After splitting the length of array is returned.

Hope this post is helpful. I share my knowledge and teach people about programming and we have more than 12k @codewithyahi Instagram family.

Since you enjoyed reading my blog, why not buy me a coffee and support my work here!! https://www.buymeacoffee.com/yajindraโ˜•

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (0)

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay