Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 150 of 377
Decoded String at Index in Python
The Decoded String at Index problem involves finding a character at a specific position in a decoded string without actually creating the full decoded string. This approach saves memory when dealing with very long decoded strings. Problem Understanding Given an encoded string, we decode it using these rules ? If the character is a letter, write it to the tape. If the character is a digit, repeat the entire current tape digit − 1 more times. For example, "hello2World3" becomes "hellohelloWorldhellohelloWorldhellohelloWorld". Algorithm Overview Instead of creating the full decoded string, we use ...
Read MoreKth Smallest Element in a Sorted Matrix in Python
Finding the Kth smallest element in a sorted matrix is a classic problem that can be solved efficiently using binary search. In this problem, we have an n x n matrix where each row and column is sorted in increasing order, and we need to find the kth smallest element in the entire matrix. Problem Understanding Given a matrix where rows and columns are sorted in ascending order, we need to find the kth smallest element. For example, in the matrix [[1, 5, 9], [10, 11, 13], [12, 13, 15]], if k=8, the answer is 13 because when ...
Read MoreSurrounded Regions in Python
The Surrounded Regions problem involves capturing all regions of 'O' that are completely surrounded by 'X' on a 2D board. A region is considered surrounded if all 'O' cells are enclosed by 'X' cells and cannot reach the board's border. Problem Understanding Given a 2D board with 'X' and 'O' characters, we need to capture surrounded regions by flipping all 'O' to 'X'. However, 'O' cells connected to the border should remain unchanged ? Input Board Output Board XXXX XOOX XXOX XOXX XXXX XXXX XXXX XOXX ...
Read MoreMinimum Path Sum in Python
The minimum path sum problem involves finding a path from the top-left corner to the bottom-right corner of a matrix that minimizes the sum of all numbers along the path. You can only move down or right at any point. Problem Example Given this matrix: 1 3 1 1 5 1 4 2 1 The optimal path is: 1 → 3 → 1 → 1 → 1, giving a minimum sum of 7. Algorithm Steps The dynamic programming approach works as follows: Fill the ...
Read MoreValidate IP Address in Python
IP address validation is a common programming task. We need to determine whether a given string is a valid IPv4 address, IPv6 address, or neither. Each format has specific rules that must be followed. IPv4 Address Rules IPv4 addresses consist of four decimal numbers (0-255) separated by dots. Leading zeros are not allowed except for "0" itself ? Valid: 192.168.1.1, 0.0.0.0 Invalid: 192.168.01.1 (leading zero), 256.1.1.1 (out of range) IPv6 Address Rules IPv6 addresses consist of eight groups of hexadecimal digits (1-4 digits each) separated by colons. Leading zeros within groups can be ...
Read MorePrint Words Vertically in Python
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 MoreCan Make Palindrome from Substring in Python
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 MoreDesign File System in Python
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 MoreNumber of Dice Rolls With Target Sum in Python
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 MoreMinimum Swaps to Group All 1's Together in Python
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