Found 10784 Articles for Python

Shortest Completing Word in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:00:10

257 Views

Suppose we have a dictionary words, and we have to find the minimum length word from a given dictionary words, it has all the letters from the string licensePlate. Now such a word is said to complete the given string licensePlate. Here, we will ignore case for letters. And it is guaranteed an answer exists. If there are more than one answers, then return answer that occurs first in the array.The license plate there may be same letter occurring multiple times. So when a licensePlate of "PP", the word "pile" does not complete the licensePlate, but the word "topper" does.So, ... Read More

Largest Number At Least Twice of Others in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:58:03

339 Views

Suppose we have an integer array called nums, now there is always exactly one largest element. We have to check whether the largest element in the array is at least twice as much as every other number in the array. If it is so, then we have to find the index of the largest element, otherwise return -1.So, if the input is like [3, 6, 1, 0], then the output will be 1, as 6 is the largest number, and for every other number in the array x, 6 is more than twice as big as x. As the index ... Read More

Min Cost Climbing Stairs in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:57:40

539 Views

Suppose there is a staircase, here the i-th step will be some non-negative cost value cost[i] assigned. When we pay the cost, we can either climb one or two steps. We have to find minimum cost to reach the top of the floor, and we also can either start from the step with index 0, or the step with index 1.So, if the input is like cost = [12, 17, 20], then the output will be 17, The cheapest position to start from step 1 as we have to pay that cost and go to the top.To solve this, we ... Read More

Find Smallest Letter Greater Than Target in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:55:28

733 Views

Suppose we have a list of sorted characters’ letters. This is containing only lowercase letters, now we have a target letter t, we have to find the smallest element in the list that is larger than the given target.And letters also wrap around. So, if the target is t = 'z' and letters = ['a', 'b'], the answer is 'a'.So, if the input is like ["c", "f", "j"], t = 'a', then the output will be 'c'.To solve this, we will follow these steps −l := 0r := size of letters - 1while l target, thenr := mid -1otherwise, ... Read More

Longest Word in Dictionary in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:55:08

367 Views

Suppose we have a list of words representing an English Dictionary, we have to find the longest word in the given word list that can be built one character at a time by other words in words. If there is more than one possible answer, then return the longest word with the smallest lexicographical order. If there is no answer, then return the empty string.So, if the input is like ["h", "he", "hel", "hell", "hello"], then the output will be "hello"To solve this, we will follow these steps −trie := a new mapDefine a function insert(). This will take wordnow ... Read More

1-bit and 2-bit Characters in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:52:21

423 Views

Suppose we have two special characters. The first character can be represented by one bit 0. And the second character can be represented by two bits like (10 or 11). So, if we have a string represented by several bits. We have to check whether the last character must be a one-bit character or not. The given string will always end with a zero.So, if the input is like [1, 0, 0], then the output will be True, as the only way to decode it is twobit character (10) and one-bit character (0). So, the last character is one-bit character.To ... Read More

Design HashMap in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:50:24

2K+ Views

Suppose we want to design a HashMap without using any built-in hash table libraries. There will be different functions as follows −put(key, value) − This will insert a value associated with key into the HashMap. If the value already exists in the HashMap, update the value.get(key) − This will return the value to which the specified key is mapped, otherwise -1 when this map contains no mapping for the key.remove(key) − This will Remove the mapping for the value key if this map contains the mapping for the key.So, if the input is like After initialization, call the put and ... Read More

Design HashSet in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:48:10

4K+ Views

Suppose we want to design a HashSet data structure without using any built-in hash table libraries. There will be different functions like −add(x) − Inserts a value x into the HashSet.contains(x) − Checks whether the value x is present in the HashSet or not.remove(x) − Removes x from the HashSet. In case the value does not exist in the HashSet, do nothing.So, to test it Initialize the hash set, then call add(1), add(3), contains(1), contains(2), add(2), contains(2), remove(2), contains(2)., then the output will be true (1 is present), false (2 is not present), true (2 is present), false (2 is ... Read More

Kth Largest Element in a Stream in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:44:48

449 Views

Suppose we want to design a class to find the kth largest element in a stream. It is the kth largest element in the sorted order, not the kth distinct element.The KthLargest class will have a constructor which accepts an integer k and an array nums, that will contain initial elements from the stream. For each call to the method KthLargest.add, will return the element representing the kth largest element in the stream.So, if the input is like k = 3, initial elements = [4, 5, 8, 2], then call add(3), add(5), add(10), add(9), add(4). , then the output will ... Read More

Employee Importance in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:38:36

192 Views

Suppose we have a data structure of employee information, there are employee's unique id, his importance value and his direct subordinates' id. As an example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. And suppose their importance values are 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []].So, if we have the employee information of a company, and an employee id, we have to find the total importance value of ... Read More

Advertisements