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
Python Articles
Page 668 of 855
Word 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 MoreLongest Valid Parentheses in Python
Finding the longest valid parentheses substring is a common problem that can be solved efficiently using a stack-based approach. Given a string containing only '(' and ')' characters, we need to find the length of the longest valid (well-formed) parentheses substring. For example, in the string "))(())())", the longest valid parentheses substring is "(())())" with length 6. Algorithm Approach We use a stack to track indices of unmatched parentheses ? Initialize a stack with −1 to handle edge cases For each character, if it's '(', push its index onto the stack If it's ')', check ...
Read MoreMerge k Sorted Lists in Python
Merging k sorted lists is a classic algorithm problem. Given multiple sorted linked lists, we need to combine them into a single sorted list. Python's heapq module provides an efficient solution using a min-heap data structure. Problem Understanding Given k sorted linked lists like [1, 4, 5], [1, 3, 4], [2, 6], we need to merge them into one sorted list [1, 1, 2, 3, 4, 4, 5, 6]. Algorithm Steps Create a min-heap to store the smallest elements from each list Add the first node of each non-empty list to the heap Repeatedly extract ...
Read MoreInsert the string at the beginning of all items in a list in Python
In this tutorial, we'll learn how to insert a string at the beginning of all items in a list in Python. For example, if we have a string "Tutorials_Point" and a list containing elements like "1", "2", "3", we need to add "Tutorials_Point" in front of each element to get "Tutorials_Point1", "Tutorials_Point2", "Tutorials_Point3". Using List Comprehension with format() The most straightforward approach is using list comprehension with string formatting − sample_list = [1, 2, 3] result = ['Tutorials_Point{0}'.format(i) for i in sample_list] print(result) ['Tutorials_Point1', 'Tutorials_Point2', 'Tutorials_Point3'] Using map() with format() ...
Read MorePython - Insert list in another list
When working with lists in Python, you often need to insert one list into another. Python provides several methods to accomplish this: append(), extend(), insert(), and list concatenation with + operator. Using append() Method The append() method adds the entire second list as a single element ? first_list = [1, 2, 3, 4, 5] second_list = [6, 7, 8, 9, 10] first_list.append(second_list) print("Using append():", first_list) Using append(): [1, 2, 3, 4, 5, [6, 7, 8, 9, 10]] Using extend() Method The extend() method adds each element of the second ...
Read More