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 38 of 377
Smallest Good Base in Python
Finding the smallest good base is a mathematical problem where we need to find the smallest base k ≥ 2 such that all digits of number n in base k are 1. For example, 121 in base 3 is 11111, so 3 is a good base for 121. Understanding the Problem If a number n has all digits as 1 in base k with m digits, then: n = 1 + k + k² + k³ + ... + k^(m-1) = (k^m - 1) / (k - 1) We need to find the smallest k ≥ 2 ...
Read MoreStrong Password Checker in Python
A strong password checker validates if a password meets specific security criteria and calculates the minimum changes needed to make it strong. Python provides several methods to check character types and implement password validation logic. Password Strength Criteria A strong password must satisfy these requirements ? Length between 6 and 20 characters At least one lowercase letter, one uppercase letter, and one digit No three consecutive repeating characters (like "aaa", "PPP", "888") Algorithm Overview The solution handles three cases based on password length ? Too short (< 6 characters): Add characters ...
Read MoreWord Search II in Python
The Word Search II problem involves finding all words from a dictionary that can be formed in a 2D character board. Each word must be constructed from letters of sequentially adjacent cells (horizontally or vertically neighboring), and the same letter cell cannot be used more than once in a single word. This problem is efficiently solved using a Trie (prefix tree) data structure combined with backtracking. The Trie allows us to prune search paths early when no dictionary word has the current prefix. Algorithm Overview The solution follows these key steps ? Build a Trie ...
Read MoreBinary Tree Postorder Traversal in Python
Binary tree postorder traversal visits nodes in the order: left subtree, right subtree, then root. This article demonstrates an iterative approach using a stack with node-state pairs to achieve postorder traversal without recursion. -10 9 10 15 7 ...
Read MoreWord Break II in Python
The Word Break II problem involves breaking a string into valid dictionary words and returning all possible sentence combinations. Given a string and a dictionary of valid words, we need to find all ways to add spaces to form valid sentences. For example, with string "appleraincoat" and dictionary ["app", "apple", "rain", "coat", "raincoat"], we can form: "apple rain coat" and "apple raincoat". Algorithm Approach We use dynamic programming with memoization to avoid redundant calculations ? Create a memoization map to store results for substrings For each position, ...
Read MoreLongest Consecutive Sequence in Python
The Longest Consecutive Sequence problem asks us to find the length of the longest sequence of consecutive integers in an unsorted array. For example, in the array [100, 4, 250, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4] with length 4. Algorithm Approach We use a set-based approach for O(n) time complexity ? Convert the array to a set for O(1) lookup operations For each number, check if it's the start of a sequence (i.e., number-1 is not in the set) If it's a sequence start, count consecutive numbers and track the ...
Read MoreBinary Tree Maximum Path Sum in Python
The maximum path sum problem asks us to find the path in a binary tree with the largest sum of node values. A path can start and end at any nodes and must follow parent-child connections. The path doesn't need to pass through the root. -10 9 10 15 7 ...
Read MoreWildcard Matching in Python
Wildcard pattern matching allows you to match strings using special characters. In wildcard matching, '?' matches any single character and '*' matches zero or more characters. This is useful for file pattern matching, search operations, and text processing. Wildcard Characters '?' − Matches exactly one character '*' − Matches zero or more characters Dynamic Programming Approach We can solve wildcard matching using dynamic programming. The idea is to build a 2D table where dp[i][j] represents whether the first i characters of the string match the first j characters of the pattern. Algorithm Steps ...
Read MoreTrapping Rain Water in Python
The trapping rain water problem is a classic algorithmic challenge where we calculate how much water can be trapped after raining on an elevation map represented by an array of heights. Each element represents the height of a bar with width 1. ...
Read MoreFirst Missing Positive in Python
The First Missing Positive problem asks us to find the smallest missing positive integer from an unsorted array. For example, given the array [4, -3, 1, -1], the result is 2 since 1 is present but 2 is missing. Algorithm Approach We use a cyclic sort approach to solve this efficiently: Add a 0 at the beginning to handle 1-based indexing Place each positive number at its correct index position Scan the array to find the first missing positive Example Implementation Here's the complete solution using cyclic sort ? class Solution: ...
Read More