1 目录 GoodArray·························································································2 How many Sentences··········································································3 Lottery Coupons·················································································4 Whole Minute Dilemma········································································6 Maximum Substring·············································································8 Shares Purchase·············································································· 10 Caplock··························································································· 12 Meandering array·············································································· 14 Is anagram······················································································ 15 Reverse sentence············································································· 16 Number of pairs················································································ 18 1 GoodArray 2 How many Sentences Given an array of words and an array of sentences, determine which words are anagrams of each other. Calculate how many sentences can be created by replacing any word with one of its anagrams. Example wordSet = ['listen', 'it', 'it', 'is'] sentence = 'listen it is silent' Determine that listen is an anagram of silent. Those two words can be replaced with their anagrams. The four sentences that can be created are: • listen it is silent • • • listen it is listen silent it is silent silent it is listen Function Description Complete the countSentences function in the editor below. 3 Lottery Coupons There is a lottery with n coupons and n people take part in it. Each person picks exactly one coupon. Coupons are numbered consecutively from 1 to n, n being the maximum ticket number. The winner of the lottery is any person who owns a coupon where the sum of the digits on the coupon is equal to s. If there are multiple winners, the prize is split equally among them. Determine how many values of s therewhere where there is at least one winner and the prize is split among most people. Example n = 12 The list of coupon numbers generated from 1 to n is [1,2,3,4,5,6,7,8,9,10,11,12]. The sums of the digits are [1,2,3, 4, 5, 6, 7,8,9, 1, 2, 3]. The largest number of winners is 2 which will occur for coupons numbered [1, 10], [2, 11] and [3, 12]. The maximum number of possible winners occurs for any of these 3 possible values of s, so 3 is the answer. Function Description Complete the function lotteryCoupons in the editor. The function must return the number of ways to choose s in such a way that there is at least one winner and the prize is split among the greatest 4 5 Whole Minute Dilemma A music player allows users to choose songs to play but it has some rules. The songs must be chosen in pairs and the pair's durations must add up to a multiple of 60 seconds (e.g., 60, 120, 180). Given a list of song durations, calculate the total number of different song pairs that can be chosen. Example n=3 songs = [40, 20, 60] Only one pair of songs has a combined duration that is a multiple of a whole minute (40+20 = 60) so return 1. While the third song is a single minute long, songs must be chosen in pairs. Function Description Complete the function playlist in the editor below. playlist has the following parameter(s): int songs[n]: the song durations in seconds Returns: int: the number of valid pairs 6 7 Maximum Substring A substring is a contiguous sequence of characters within a stringGiven a string determine the alphabetically maximum substring Example s = 'baca' The unique substrings: ['b', 'bac', 'baca', 'a', 'ac', 'aca', 'c', 'c', 'c Arranging the substrings alphabetically: ['a', 'ac', 'aca', 'b', 'ba', 'bSac', 'baca', 'ca'] The maximum substring alphabetically: 'ca Function Description Complete the function maxSubstring in the editor below. maxSubstring has the following parameter(s): string s: a string Returns string: the maximum substring in s Constraints 1<length ofs 100 all characters of s are in the range ascii[a-z] 8 9 Shares Purchase Every year, an investment firm invests into several companies' shares. Marority of their investments are in 3 companies: A, B and C. As they inveyesey create a string of all of their investments in order. Given a string of their investments, determine the number of time period:s they invested in the three major companies'shares For example,given the investments as s = "ABBCZBAC", for total number of investments n=7. Starting from the left, the first substringthat contains an investment in all companies is "ABBC". There are are13 substrings of swhich meet the criterion: ["ABBC", "ABBCZ", "ABBCZ"B","АBBCZBA", "ABBCZBAC", "BBCZBA", "BBCZBAC", "BCZ"BAC", "CZBA", "CZBAC", "ZBAC" Note: Two substrings are considered different if the starting, ending, orboth positions differ. Function Description Complete the function analyzelnvestments in the editor below. Thefunction must return a long integer. analyzelnvestments has the following parameter(s): s: a string of length n Constraints • 1sns105 • s[i]E{'A'...'Z'}(where 0 s i < n) Example 1: Input: "ABC" Output: 1 10 Example 2: Input: "ABCCBA" Output: 7 Example 3: Input: "PQACBA" Output: 7 11 Caplock 12 13 Meandering array Took the Hackerrank test for Goldman Sachs NYC New Analyst Engineering Program today. Both questions were really easy in my opinion. Question : Meandering array An array of integers is defined as being meandering order when the first two elements are the respective largest and smallest elements in the array and the subsequent elements alternate between its next largest and next smallest elements. In other words, the elements are in order of [first_largest, first_smallest, second_largest, second_smallest.....] Example: The array [-1, 1, 2, 3, -5] sorted normally is [-5, -1, 1, 2, 3]. Sorted in meandering order, it becomes [3, -5, 2, -1, 1] Constraints: 2 <= n <= 10^4 -10^6 <= unsorted[i] <= 10^6 The unsorted array may contain duplicate elements 14 Is anagram Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Example 1: Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]] Example 2: Input: strs = strs = [""] Output: [[""]] Example 3: Input: strs = strs = ["a"] Output: [["a"]] 15 Reverse sentence Given a String S, reverse the string without reversing its individualwordsWords are separated by dots. Input Format The input consists of a single line containing a string 'S'. Output Format: The output format contains a string which is the reversed version of the given string 'S' Given: i.like.this.program.very.much Output: much.very.program.this.like.i Explanation of given Test Cases : After reversing the whole string(not individual words),the input string becomes much. very.program.this.like.i Approach: • Initially, reverse the individual words of the given string oneby one, for the above example, after reversing individual words the string shhould be "i.ekil.siht.margorp.yrev. hcum". • Reverse the whole string from start to end to get the desired output "much.very. program.this.like.i" in the above example. 16 Code: 17 Number of pairs Given an array of integers, and an integer 'K', find the courint of pairs of elements in the array whose sum is equal to 'K'. Input: First line of the input contains an integer T,denoting the nuimber of test cases. Then T test cases follow. Each test case consists of twolines. First line of each test case contains 2 space separated integers Nand K denoting the size of array and the sum respectively. Second line of each test case cortains N space separated integers denoting the elements of the array. Output: Print the count of pairs of elements in the array whose sum isequal to the K. Constraints: 1<=T<=50 1<=N<=50 1<=K<=50 1<=A[i]<=100 Example: Input 2 46 1571 42 1111 Output 2 6 18 19