CategoryPython

HackerRank âSherlock and Pairsâ Solution

H

Short Problem Definition: Sherlock is given an array of N integers A0, A1 âŚ AN-1 by Watson. Now Watson asks Sherlock how many different pairs of indices i and j exist such that i is not equal to j but Ai is equal to Aj. That is, Sherlock has to count total number of pairs of indices (i, j) where Ai = Aj AND i â  j. Link Sherlock and Pairs Complexity: time complexity is O(n) space complexity is...

HackerRank âSherlock and Squaresâ Solution

H

Short Problem Definition: Watson gives two integers A & B to Sherlock and asks if he can count the number of square integers between A and B (both inclusive). A square integer is an integer which is the square of any integer. For example, 1, 4, 9, 16 are some of the square integers as they are squares of 1, 2, 3, 4 respectively. Link Sherlock and Squares Complexity: time complexity is O(sqrt(N));...

HackerRank âSherlock and The Beastâ Solution

H

Short Problem Definition: Sherlock Holmes suspects his archenemy, Professor Moriarty, is once again plotting something diabolical. Sherlockâs companion, Dr. Watson, suggests Moriarty may be responsible for MI6âs recent issues with their supercomputer, The Beast. Shortly after resolving to investigate, Sherlock receives a note from Moriarty boasting about infecting The Beastwith a virus; however...

HackerRank âSherlock and The Valid Stringâ Solution

H

Short Problem Definition: Sherlock considers a string to be valid if all characters of the string appear the same number of times. It is also valid if he can remove just 1 character at 1 index in the string, and the remaining characters will occur the same number of times. Given a string , determine if it is valid. If so, return YES, otherwise...

HackerRank âSherlock and Valid Stringâ Solution

H

Short Problem Definition: Sherlock considers a string to be valid if all characters of the string appear the same number of times. It is also valid if he can remove just 1 character at 1 index in the string, and the remaining characters will occur the same number of times. Given a string , determine if it is valid. If so, return YES, otherwise...

HackerRank âSherlock and Watsonâ Solution

H

Short Problem Definition: John Watson performs an operation called Right Circular Rotation on an integer array [a0, a1, âŚ an-1]. Right Circular Rotation transforms the array from [a0, a1, âŚ aN-1] to [aN-1, a0,âŚ aN-2]. He performs the operation K times and tests Sherlockâs ability to identify the element at a particular position in the array. He asks Q queries. Each query consists of one integer...

HackerRank âPairsâ Solution

H

Short Problem Definition: Given N integers, count the number of pairs of integers whose difference is K. Link Pairs Complexity: time complexity is O(N*log(N)) space complexity is O(N) Execution: The solution is pretty straight-forward, just read the code :). The runtime complexity is calculated with log(N) access times for tree-based sets (not the case in Python). Solution: 1 2 3 4 5 6 7 8 9...

HackerRank âSimple Text Editorâ Solution

H

Short Problem Definition: In this problem, your task is to implement a simple text editor. Initially, a file contains an empty string S. Your task is to perform Q operations consisting of the following 4 types: append(W) â Appends the string W at the end of S. erase(k) â Erase the last k character of S. get(k) â Returns the kth character of S. undo() â Undo the last not previously undone...

HackerRank âPalindrome Indexâ Solution

H

Short Problem Definition: You are given a string of lower case letters. Your task is to figure out the index of the character on whose removal it will make the string a palindrome. There will always be a valid solution. Link Palindrome Index Complexity: time complexity is O(N) space complexity is O(N) Execution: The solution seems n^2 but isPalindrome is executed only once. Due to the problem...

HackerRank âSock Merchantâ Solution

H

Short Problem Definition: John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. Link Sock Merchant Complexity: time complexity is O(N) space complexity is O(N) Execution: Group the same color together. Look out for the...