Printing words vertically means taking each character at the same position from different words and forming new strings. For example, if we have "HOW ARE YOU", we take the first character from each word (H, A, Y) to form "HAY", second characters (O, R, O) to form "ORO", and so on. Algorithm Steps To solve this problem, we follow these steps − Split the input string into words Find the maximum length among all words (this determines how many vertical strings we need) For each position, collect characters from all words at that position Add spaces ... Read More
Given a string s, we need to determine if substrings can be made into palindromes after performing certain operations. For each query [left, right, k], we can rearrange the substring s[left:right+1] and replace up to k characters. The goal is to check if a palindrome is possible after these operations. The key insight is that in a palindrome, at most one character can have an odd frequency (the middle character). So we need to count characters with odd frequencies and see if we can fix them with our allowed replacements. Algorithm We'll use a prefix sum approach ... Read More
A file system data structure allows us to create paths and associate values with them. We need to implement two main operations: creating a path with a value and retrieving the value associated with a path. Problem Requirements We need to design a file system that provides these two functions: createPath(path, value) − Creates a new path and associates a value to it if possible and returns True. Returns False if the path already exists or its parent path doesn't exist. get(path) − Finds the value associated with ... Read More
The problem asks us to find the number of ways to roll d dice (each with f faces) such that the sum equals a target value. We need to return the result modulo 109 + 7. For example, with 2 dice having 6 faces each and target sum 7, there are 6 ways: (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1). Algorithm Approach We'll use dynamic programming where dp[i][j] represents the number of ways to achieve sum j using i+1 dice ? Base case: For the first die, there's exactly 1 ... Read More
In a binary array, we often need to group all 1's together with minimum swaps. This problem uses a sliding window approach with prefix sums to find the optimal position for grouping all 1's. Algorithm Approach The key insight is to use a sliding window of size equal to the total count of 1's. We find the window position that already contains the maximum number of 1's, minimizing the swaps needed. Steps Count total number of 1's in the array Create a prefix sum array for efficient range sum queries Use sliding window of size ... Read More
A Snapshot Array is a data structure that allows you to take snapshots of an array at different points in time and retrieve values from those snapshots. This is useful when you need to track the history of changes to array elements. Interface Requirements The SnapshotArray must support these operations: SnapshotArray(int length) − Initialize array with given length, all elements start as 0 set(index, val) − Set element at given index to val snap() − Take a snapshot and return snapshot ID (starts from ... Read More
The Binary Tree Coloring Game is a two-player strategy game played on a binary tree. Each player colors nodes to control territory, and the winner is determined by who colors more nodes. As the second player, we need to determine if we can guarantee a win by choosing the optimal starting node. Game Rules The game works as follows: Player 1 chooses node x and colors it red Player 2 chooses node y and colors it blue Players alternate turns coloring adjacent uncolored nodes The game ends when no moves are possible Winner is the player who ... Read More
Suppose we have an array of integers, where a move operation involves choosing any element and decreasing it by 1. An array is a zigzag array if it satisfies either of these patterns: Every even-indexed element is greater than adjacent elements: A[0] > A[1] < A[2] > A[3] < A[4] > ... and so on. Every odd-indexed element is greater than adjacent elements: A[0] < A[1] > A[2] < A[3] > A[4] < ... and so on. We need to find the minimum number of moves to transform the given array into a zigzag array. ... Read More
Connecting cities with minimum cost is a classic Minimum Spanning Tree (MST) problem. Given N cities and connections with costs, we need to find the minimum cost to connect all cities. This can be solved using Kruskal's algorithm with a Union-Find data structure. Problem Understanding We have N cities numbered from 1 to N, and connections where each connection [city1, city2, cost] represents the cost to connect two cities. We need to find the minimum cost to ensure every pair of cities has a path between them ? ... Read More
Suppose we have a directed graph with nodes labelled 0, 1, ..., n-1. In this graph, each edge is colored with either red or blue colors, and there could be self-edges or parallel edges. Each [i, j] in red_edges indicates a red directed edge from node i to node j. Similarly, each [i, j] in blue_edges indicates a blue directed edge from node i to node j. We need to find an array answer of length n, where each answer[X] is the length of the shortest path from node 0 to node X such that the edge colors alternate along ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance