diff --git a/01. Arrays and strings/hackerrank/making_anagrams.py b/01. Arrays and strings/hackerrank/making_anagrams.py new file mode 100644 index 0000000..38f5ff7 --- /dev/null +++ b/01. Arrays and strings/hackerrank/making_anagrams.py @@ -0,0 +1,26 @@ +# Algorithms > Strings > Making Anagrams +# https://www.hackerrank.com/challenges/making-anagrams + +import unittest +from collections import Counter + +def makingAnagrams(s1, s2): + counts = Counter(s1) + counts.subtract(s2) + return sum(abs(x) for x in counts.values()) + +class Test(unittest.TestCase): + + data = [(('a', 'b'), 2), + (('ab', 'ab'), 0), + (('abc', 'amnop'), 6), + (('cde', 'abc'), 4), + (('cde', 'abcc'), 5)] + + def test(self): + for test_string, expected in self.data: + res = makingAnagrams(*test_string) + self.assertEqual(res, expected) + +if __name__ == "__main__": + unittest.main() diff --git a/01. Arrays and strings/hackerrank/making_anagrams2.py b/01. Arrays and strings/hackerrank/making_anagrams2.py new file mode 100644 index 0000000..07abcfe --- /dev/null +++ b/01. Arrays and strings/hackerrank/making_anagrams2.py @@ -0,0 +1,29 @@ +# Algorithms > Strings > Anagram +# https://www.hackerrank.com/challenges/anagram + +import unittest +from collections import Counter + +def anagram(s): + if len(s) % 2 == 1: + return -1 + counts = Counter(s[:len(s)//2]) + counts.subtract(s[len(s)//2:]) + return sum(abs(x) for x in counts.values())//2 + +class Test(unittest.TestCase): + + data = [('aaabbb', 3), + ('ab', 1), + ('abc', -1), + ('mnop', 2), + ('xyyx', 0), + ('xaxbbbxx', 1)] + + def test(self): + for test_string, expected in self.data: + res = anagram(test_string) + self.assertEqual(res, expected) + +if __name__ == "__main__": + unittest.main() diff --git a/01. Arrays and strings/hackerrank/strong_password.py b/01. Arrays and strings/hackerrank/strong_password.py new file mode 100644 index 0000000..3b90f5c --- /dev/null +++ b/01. Arrays and strings/hackerrank/strong_password.py @@ -0,0 +1,35 @@ +# Algorithms > Strings > Strong Password +# https://www.hackerrank.com/challenges/strong-password + +import unittest + +def minimumNumber(s): + numbers = "0123456789" + lower_case = "abcdefghijklmnopqrstuvwxyz" + upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + special_characters = "!@#$%^&*()-+" + + count = 0 + if not any(x in numbers for x in s): + count += 1 + if not any(x in lower_case for x in s): + count += 1 + if not any(x in upper_case for x in s): + count += 1 + if not any(x in special_characters for x in s): + count += 1 + + return max(count, 6-len(s)) + +class Test(unittest.TestCase): + + data = [('Ab1', 3), + ('#HackerRank', 1)] + + def test(self): + for test_string, expected in self.data: + res = minimumNumber(test_string) + self.assertEqual(res, expected) + +if __name__ == "__main__": + unittest.main()