Uncommon Words from Two Sentences Problem

Problem A sentence is a string of single-space separated words where each word consists only of lowercase letters. A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence. Given two sentences s1 and s2, return a list of all the uncommon words. You may return the answer in any order. Examples Example 1: ...

Count the Number of Consistent Strings Problem

Problem You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed. Return the number of consistent strings in the array words. Examples Example 1: Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"] Output: 2 Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'. ...

Minimum Steps to Traverse Points in an Infinite 2D Grid Problem

Problem You are in an infinite 2D grid where you can move in any of the 8 directions: (x,y) to (x+1, y), (x - 1, y), (x, y+1), (x, y-1), (x-1, y-1), (x+1,y+1), (x-1,y+1), (x+1,y-1) ...

Capturing Loop Variables in Lambdas Problem

Problem What does the below code snippet print out? How can we fix the anonymous functions to behave as we’d expect? functions = [] for i in range(10): functions.append(lambda : i) for f in functions: print(f()) Solution Method 1 - Using default argument at creation The given code snippet will print the number 9 ten times. This happens because the anonymous functions (lambdas) capture the variable i by reference. When the functions are eventually called in the print loop, they all refer to the final value that i held in the for loop, which is 9. ...

Read N characters given read7

Problem Using a read7() method that returns 7 characters from a file, implement readN(n) which reads n characters. For example, given a file with the content “Hello world”, three read7() returns “Hello w”, “orld” and then “”. Solution Method 1 - Using Iteration Here are the steps: Buffer Management: Maintain a buffer to store characters read by read7() that weren’t consumed by readN(n) calls. Reading n Characters: Continuously read using read7() until we have at least n characters in total. If read7() returns fewer than 7 characters, we’ve likely reached the end of the file. Serve the Required Characters: Serve characters from the buffer to fulfill the readN(n) request. Code Java public class FileReader { private String content; private int index; public FileReader(String content) { this.content = content; this.index = 0; } public String read7() { if (index >= content.length()) { return ""; } int end = Math.min(index + 7, content.length()); String result = content.substring(index, end); index += 7; return result; } } public class Solution { private FileReader fileReader; private Queue<Character> buffer; public Solution(FileReader fileReader) { this.fileReader = fileReader; this.buffer = new LinkedList<>(); } public String readN(int n) { StringBuilder result = new StringBuilder(); while (result.length() < n) { // If buffer is empty, read more characters using read7() if (buffer.isEmpty()) { String readData = fileReader.read7(); if (readData.isEmpty()) { break; } for (char c : readData.toCharArray()) { buffer.offer(c); } } // Add buffered characters to the result while (!buffer.isEmpty() && result.length() < n) { result.append(buffer.poll()); } } return result.toString(); } } ...

Sum of Digits of String After Convert Problem

Problem You are given a string s consisting of lowercase English letters, and an integer k. First, convert s into an integer by replacing each letter with its position in the alphabet (i.e., replace 'a' with 1, 'b' with 2, …, 'z' with 26). Then, transform the integer by replacing it with the sum of its digits. Repeat the transform operation k times in total. For example, if s = "zbax" and k = 2, then the resulting integer would be 8 by the following operations: ...

Convert 1D Array Into 2D Array Problem

Problem You are given a 0-indexed 1-dimensional (1D) integer array original, and two integers, m and n. You are tasked with creating a 2-dimensional (2D) array with m rows and n columns using all the elements from original. The elements from indices 0 to n - 1 (inclusive) of original should form the first row of the constructed 2D array, the elements from indices n to 2 * n - 1 (inclusive) should form the second row of the constructed 2D array, and so on. ...

Lemonade Change Problem

Problem At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5. Note that you do not have any change in hand at first. ...

Efficient Conversion Between Imperial Distance Units Problem

Problem The United States uses the imperial system of weights and measures, which means that there are many different, seemingly arbitrary units to measure distance. There are 12 inches in a foot, 3 feet in a yard, 22 yards in a chain, and so on. Create a data structure that can efficiently convert a certain quantity of one unit to the correct amount of any other unit. You should also allow for additional units to be added to the system. ...

Kth Distinct String in an Array Problem

Problem A distinct string is a string that is present only once in an array. Given an array of strings arr, and an integer k, return the kth distinct string present in arr. If there are fewer than k distinct strings, return an empty string "". Note that the strings are considered in the order in which they appear in the array. Examples Example 1: ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy