HackerRank 'CamelCase' Solution

Martin Kysel · September 13, 2018

Short Problem Definition:

Alice wrote 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 s , print the number of words in s on a new line.

For example, s = OneTwoThree . There are 3 words in the string.

CamelCase

Complexity:

time complexity is O(N)

space complexity is O(1)

Execution:

Since the input always has at least 1 character, we can assume that there will always be at least one word. Each upper case later identifies the next word. So the result is number of capital letters + 1.

Solution:

s = raw_input().strip()

cnt = 1

for c in s:
    if c.isupper():
        cnt += 1
        
print cnt

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.