# Daily HackerRank Challenge - Day 4

Daily HackerRank Challenge (30 Part Series)

This is a series of Daily HackerRank Challenge. Each day I show the some solutions written in C++.

# Camel Case

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

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

Sample Input

saveChangesInTheEditor


Sample Output

5


Explanation

String contains five words:

save
Changes
In
The
Editor
Thus, we print 5 on a new line.


Read an input s and loop each character. If it is in upper case, add cnt by 1. Output the cnt at the end.

Final Solution

int cnt=1; string s;

int main()
{
FAST_INP;

cin >> s;
REP(i,s.size()){
if(isupper(s[i])) cnt++;
}
cout << cnt;
return 0;
}


# Making Anagrams

We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency. For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.

Alice is taking a cryptography class and finding anagrams to be very useful. She decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?

Given two strings, s1 and s2, that may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings.

For example, s1 = abc and s2=amnop . The only characters that match are the a's so we have to remove bc from s1 and mnop from s2 for a total of 6 deletions.

Sample Input

cde
abc


Sample Output

4


Explanation

We delete the following characters from our two strings to turn them into anagrams of each other:

Remove d and e from cde to get c.
Remove a and b from abc to get c.
We had to delete  characters to make both strings anagrams.


Let's say

s1: aaaaaabc
s2: aaaaabbc
r : aaaaabc


and if we break it down

s1            | a:6 b:1 c:1
s2            | a:5 b:2 c:1
expected      | a:5 b:1 c:1
to-be-deleted | a:1 b:1 c:0


We can easily find the pattern so that we know how many and which letters we need to delete. For each letter, the pattern is

|s1(n)-s2(n)|


We need the absolute value or else we will get a negative value and make the calculation wrong. At the end, sum them up and output it.

Read two inputs s1 and s2.

For each letter,

for(int c='a';c<='z';++c)


we can use count to find out the occurrence of the letter

// for s1
count(s1.begin(),s1.end(),c)
// for s2
count(s2.begin(),s2.end(),c))


use abs to return the absolute value

Final Solution

string s1,s2;
int r=0;

int main()
{
FAST_INP;

cin >> s1 >> s2;
for(int c='a';c<='z';++c){
r+=abs(count(s1.begin(),s1.end(),c) - count(s2.begin(),s2.end(),c));
}
cout << r;
return 0;
}


Louise joined a social networking site to stay in touch with her friends. The signup page required her to input a name and a password. However, the password must be strong. The website considers a password to be strong if it satisfies the following criteria:

Its length is at least 6.
It contains at least one digit.
It contains at least one lowercase English character.
It contains at least one uppercase English character.
It contains at least one special character. The special characters are: !@#$%^&*()-+ She typed a random string of length in the password field but wasn't sure if it was strong. Given the string she typed, can you find the minimum number of characters she must add to make her password strong? Note: Here's the set of types of characters in a form you can paste in your solution: numbers = "0123456789" lower_case = "abcdefghijklmnopqrstuvwxyz" upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" special_characters = "!@#$%^&*()-+"


Sample Input

3
Ab1


Sample Output

3


Explanation

She can make the password strong by adding 3 characters, for example, $hk, turning the password into Ab1$hk which is strong.
2 characters aren't enough since the length must be at least 6.


Read n and loop n times to read each letter c.

Make 5 variables to hold if each condition is fulfilled or not.

l  ->    Its length is at least 6.
d  ->    It contains at least one digit.
lc ->    It contains at least one lowercase English character.
uc ->    It contains at least one uppercase English character.
sc ->    It contains at least one special character. The special characters are: !@#$%^&*()-+  Initialise all 5 variables to 0. Set it to 1 if the condition is fulfilled. We also need another variable x to store the number of conditions that are not fulfilled except the first condition. For example, Ab1 does not contain at least one special character. Hence, x is 1. Ab1 l d lc uc sc x 0 1 1 1 0 1  Imagine if the length of the input is at least 6, we can simply add 1 special character for this case. If x is 2, we can add two. Therefore, for l=1 case, we need to add x characters. However, if the length of the input is less than 6, we need to add at least 6-n characters. This does not cover all cases. Let's say n is 4 while x is 4 We cannot just add 2 characters to cover 4 cases. Therefore, for l=1, we need to add max(x, 6-n) characters. Final Solution int n,x,l,d,lc,uc,sc; char c; int main() { FAST_INP; cin >> n; x=l=d=lc=uc=sc=0; if(n>=6) l=1; REP(i,n){ cin >> c; if(isdigit(c)) d=1; else if(islower(c)) lc=1; else if(isupper(c)) uc=1; else if(c=='!'||c=='@'||c=='#'||c=='$'||c=='%'||c=='^'||c=='&'||c=='*'||c=='('||c==')'||c=='-'||c=='+') sc=1;
}

if(!d)++x;
if(!lc)++x;
if(!uc)++x;
if(!sc)++x;

if(!l) cout << max(x, 6-n);
else  cout << x;

return 0;
}


# Complete Code

Check out the complete code via below links

Daily HackerRank Challenge (30 Part Series)

Posted on by:

### Wing-Kam

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.