Strong Password, by HackerRank
It has been a while since I solved a HackerRank problem (they have not been posting new problems, now apparently there is a wave of new ones coming), here it is https://www.hackerrank.com/challenges/strong-password/problem:
There isn't a lot to "optimize" in the solution for this problem: pretty much just follow the 5 points mentioned in the problem description. There is some minor math to worry about, but other than that this is the kind of problem whose description is telling you how to solve it (just like those songs whose lyrics tell you how to dance to them ;)). Code is below, many hugs, Marcelo.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static int minimumNumber(int n, string password)
{
// Return the minimum number of characters to make the password strong
if (password.Length != n) return 0;
string numbers = "0123456789";
string lower_case = "abcdefghijklmnopqrstuvwxyz";
string upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string special_characters = "!@#$%^&*()-+";
int retVal = 0;
bool containsDigit = false;
bool containsLowercase = false;
bool containsUppercase = false;
bool containsSpecial = false;
for (int i = 0; i < n; i++)
{
if (numbers.Contains(password[i])) containsDigit = true;
if (lower_case.Contains(password[i])) containsLowercase = true;
if (upper_case.Contains(password[i])) containsUppercase = true;
if (special_characters.Contains(password[i])) containsSpecial = true;
}
if (!containsDigit) retVal++;
if (!containsLowercase) retVal++;
if (!containsUppercase) retVal++;
if (!containsSpecial) retVal++;
if (n + retVal < 6)
{
retVal += (6 - n - retVal);
}
return retVal;
}
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
string password = Console.ReadLine();
int answer = minimumNumber(n, password);
Console.WriteLine(answer);
}
}
There isn't a lot to "optimize" in the solution for this problem: pretty much just follow the 5 points mentioned in the problem description. There is some minor math to worry about, but other than that this is the kind of problem whose description is telling you how to solve it (just like those songs whose lyrics tell you how to dance to them ;)). Code is below, many hugs, Marcelo.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static int minimumNumber(int n, string password)
{
// Return the minimum number of characters to make the password strong
if (password.Length != n) return 0;
string numbers = "0123456789";
string lower_case = "abcdefghijklmnopqrstuvwxyz";
string upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string special_characters = "!@#$%^&*()-+";
int retVal = 0;
bool containsDigit = false;
bool containsLowercase = false;
bool containsUppercase = false;
bool containsSpecial = false;
for (int i = 0; i < n; i++)
{
if (numbers.Contains(password[i])) containsDigit = true;
if (lower_case.Contains(password[i])) containsLowercase = true;
if (upper_case.Contains(password[i])) containsUppercase = true;
if (special_characters.Contains(password[i])) containsSpecial = true;
}
if (!containsDigit) retVal++;
if (!containsLowercase) retVal++;
if (!containsUppercase) retVal++;
if (!containsSpecial) retVal++;
if (n + retVal < 6)
{
retVal += (6 - n - retVal);
}
return retVal;
}
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
string password = Console.ReadLine();
int answer = minimumNumber(n, password);
Console.WriteLine(answer);
}
}
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteSolution in Java
Deletehttps://drive.google.com/file/d/1nm3eQGt2Prz9-bxYIf-IRtie8xnBoHQW/view?usp=sharing