Python Articles

Page 677 of 855

Count all prefixes in given string with greatest frequency using Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 694 Views

In this tutorial, we will write a Python program that finds all prefixes of a string where one character appears more frequently than another character. This is useful for analyzing character frequency patterns in strings. Given a string and two characters, we need to find all prefixes where the first character has a higher frequency than the second character, then display the count of such prefixes. Examples Example 1 For string "apple" with characters 'p' and 'e' − Input: string = "apple", char1 = 'p', char2 = 'e' Output: ap app appl apple ...

Read More

Increasing Triplet Subsequence in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 675 Views

An increasing triplet subsequence is a sequence of three numbers from an array where each number is smaller than the next one, and they appear in the same order as in the original array (but not necessarily consecutive). The problem asks us to determine if such a triplet exists in an unsorted array. Problem Definition Given an array, return True if there exists indices i, j, k such that: arr[i] < arr[j] < arr[k] 0 ≤ i < j < k ≤ n-1 Otherwise, return False. Algorithm Approach We use a ...

Read More

Odd Even Linked List in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 780 Views

An odd-even linked list groups all odd-positioned nodes together followed by even-positioned nodes. The positions are 1-indexed, so the 1st, 3rd, 5th nodes are odd positions, and 2nd, 4th, 6th are even positions. For example, if we have nodes [1, 22, 13, 14, 25], the result will be [1, 13, 25, 22, 14] where odd-positioned nodes (1, 13, 25) come first, followed by even-positioned nodes (22, 14). Algorithm To solve this problem efficiently in-place ? If head is null or has only one node, return head Use ...

Read More

Longest Increasing Subsequence in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 3K+ Views

The Longest Increasing Subsequence (LIS) problem asks us to find the length of the longest subsequence in an array where elements are in strictly increasing order. For example, in the array [10, 9, 2, 5, 3, 7, 101, 18], the LIS is [2, 3, 7, 101] with length 4. Algorithm Overview We'll use a binary search approach with O(n log n) time complexity ? Create a tails array to store the smallest tail element for each possible LIS length For each number, use binary search to find its correct position Update the tails array and track ...

Read More

Find the Duplicate Number in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 3K+ Views

When you have an array containing n + 1 integers where each number is between 1 and n, there must be at least one duplicate number. This problem can be solved efficiently using Floyd's Cycle Detection Algorithm (also known as the tortoise and hare algorithm). The key insight is to treat the array as a linked list where each element points to the index of its value. Since there's a duplicate, there will be a cycle in this "linked list". Algorithm Steps The solution works in two phases ? Phase 1: Detect if a cycle ...

Read More

Search a 2D Matrix II in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 727 Views

When working with a 2D matrix where rows and columns are sorted in ascending order, we need an efficient search algorithm. This problem is commonly known as "Search a 2D Matrix II" and can be solved optimally using a staircase search approach. Matrix Properties The matrix has these important characteristics: Integers in each row are sorted in ascending order from left to right Integers in each column are sorted in ascending order from top to bottom Here's an example matrix: ...

Read More

Product of Array Except Self in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

The Product of Array Except Self problem requires finding an array where each element is the product of all other elements in the original array, without using division. For input [1, 2, 3, 4], the output should be [24, 12, 8, 6]. Algorithm Overview The solution uses two passes to calculate left and right products efficiently ? Create a right_mul array to store cumulative products from right to left Use a prefix variable to track left products while building the final result Combine left ...

Read More

Kth Smallest Element in a BST in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Finding the Kth smallest element in a Binary Search Tree (BST) is a common problem that leverages the BST property where in-order traversal visits nodes in sorted order. We can use in-order traversal to collect elements and return the Kth smallest one. 10 5 15 2 7 13 ...

Read More

Implement Trie (Prefix Tree) in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 5K+ Views

A Trie (prefix tree) is a tree data structure used to efficiently store and search strings. It's particularly useful for autocomplete features and prefix matching. Each node represents a character, and paths from root to nodes represent prefixes. Trie Structure Overview A Trie supports three main operations ? insert(word) − Adds a word to the trie search(word) − Returns True if the complete word exists startsWith(prefix) − Returns True if any word starts with the given prefix Implementation We'll use a ...

Read More

Number of Islands in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

The Number of Islands problem involves counting connected components of land (represented by 1s) in a 2D grid surrounded by water (represented by 0s). An island is formed by connecting adjacent lands horizontally or vertically. Consider this grid example ? 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1 This grid contains three islands: one large island (blue), one single-cell island (orange), and one small island (pink). Algorithm Overview The solution uses Depth-First Search ...

Read More
Showing 6761–6770 of 8,546 articles
« Prev 1 675 676 677 678 679 855 Next »
Advertisements