09/13/18

# HackerRank ‘CamelCase’ Solution

##### 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


If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

09/12/18

# HackerRank ‘Super Reduced String’ Solution

##### Short Problem Definition:

Steve has a string of lowercase characters in range ascii[‘a’..’z’]. He wants to reduce the string to its shortest length by doing a series of operations. In each operation he selects a pair of adjacent lowercase letters that match, and he deletes them. For instance, the string aab could be shortened to b in one operation.

Steve’s task is to delete as many characters as possible using this method and print the resulting string. If the final string is empty, print Empty String

##### Link

Super Reduced String

##### Complexity:

time complexity is O(N)

space complexity is O(N)

##### Execution:

The solution creates a stack of values. If the top value on the stack is equivalent to the next value, simply remove both. This solution assumes that there are only ever 2 values next to each other. If any adjacent values were to be removed, I would require more loops.

##### Solution:
i = raw_input()

s = []

for c in i:
if not s:
s.append(c)
else:
if s[-1] == c:
s.pop()
else:
s.append(c)

if not s:
print "Empty String"
else:
print ''.join(s)


If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.