Python Articles

Page 671 of 855

Palindromic Substrings in Python

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

A palindromic substring is a substring that reads the same forwards and backwards. Given a string, we need to count all palindromic substrings where substrings with different start or end indices are counted separately, even if they contain the same characters. For example, in the string "aaa", there are 6 palindromic substrings: "a", "a", "a", "aa", "aa", "aaa". Brute Force Approach The simplest approach is to check every possible substring and verify if it's a palindrome ? class Solution: def countSubstrings(self, s): counter ...

Read More

Minimum Moves to Equal Array Elements II in Python

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

Finding the minimum number of moves to make all array elements equal is a classic problem that can be solved efficiently using the median approach. A move consists of incrementing or decrementing any element by 1. Algorithm Overview The key insight is that the optimal target value is the median of the array. This minimizes the total distance (moves) needed to make all elements equal ? Steps to Solve Sort the array to find the median easily Find the median element (middle element of sorted array) ...

Read More

4Sum II in Python

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

The 4Sum II problem asks us to find how many tuples (i, j, k, l) exist such that A[i] + B[j] + C[k] + D[l] equals zero, given four lists A, B, C, D of integers. This is an optimization problem that can be solved efficiently using a hash map approach. Problem Understanding Given four lists of integers with the same length N (0 ≤ N ≤ 500), we need to count tuples where the sum of elements at indices (i, j, k, l) from lists A, B, C, D respectively equals zero. For example, with A = ...

Read More

Hexspeak in Python

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

Hexspeak is a novelty form of spelling that uses hexadecimal digits to represent words. In Python, we can convert decimal numbers to Hexspeak by first converting to hexadecimal, then replacing specific digits with letters. Understanding Hexspeak Conversion The conversion process involves these steps: Convert decimal number to hexadecimal Replace digit 0 with letter 'O' and digit 1 with letter 'I' Keep hexadecimal letters A-F unchanged If any other digits (2-9) exist, return "ERROR" Valid Hexspeak characters are: {"A", "B", "C", "D", "E", "F", "I", "O"} Implementation Using Built-in hex() Function Here's a ...

Read More

Diet Plan Performance in Python

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

A diet plan performance problem tracks a dieter's points based on their calorie consumption over consecutive k days. The dieter gains or loses points depending on whether their total calories fall below a lower bound, above an upper bound, or within the normal range. Problem Description Given an array calories[i] representing daily calorie consumption, we need to evaluate every consecutive sequence of k days and calculate points based on these rules: If total calories < lower bound: lose 1 point If total calories > upper bound: gain 1 point Otherwise: no change in points ...

Read More

Single-Row Keyboard in python

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

A single-row keyboard requires finger movement between keys to type characters. Given a keyboard layout string and a word, we need to calculate the total time to type the word, where time equals the absolute distance between character positions. Problem Understanding Consider a keyboard layout "abcdefghijklmnopqrstuvwxyz" and word "hello": Start at index 0 (position 'a') Move to 'h' (index 7): distance = |0 - 7| = 7 Move to 'e' (index 4): distance = |7 - 4| = 3 Move to 'l' (index 11): distance = |4 - 11| = 7 Stay at 'l' (index 11): ...

Read More

Check If a Number Is Majority Element in a Sorted Array in Python

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

A majority element in an array is an element that appears more than N/2 times in an array of length N. When dealing with a sorted array, we can use binary search to efficiently find the first and last occurrence of the target element and check if its frequency exceeds the majority threshold. Algorithm Overview To solve this problem, we need to: Find the leftmost (first) occurrence of the target using binary search Find the rightmost (last) occurrence of the target using binary search ...

Read More

Largest Unique Number in Python

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

In this problem, we need to find the largest number that appears exactly once in a list. If no such number exists, we return -1. Problem Understanding Given a list like [5, 2, 3, 6, 5, 2, 9, 6, 3], we need to ? Count the frequency of each number Find numbers that appear exactly once Return the largest among those unique numbers Method 1: Using Dictionary for Counting We can use a dictionary to count occurrences and then find the largest unique number ? def largest_unique_number(numbers): ...

Read More

Remove Vowels from a String in Python

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

Removing vowels from a string is a common string manipulation task in Python. We can accomplish this using several approaches like replace(), list comprehension, or regular expressions. Using replace() Method The simplest approach is to use the replace() method to remove each vowel one by one ? def remove_vowels(s): vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] for vowel in vowels: s = s.replace(vowel, "") return s text = "iloveprogramming" result ...

Read More

Two Sum Less Than K in Python

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

The Two Sum Less Than K problem asks us to find the maximum sum of two distinct elements in an array that is less than a given value K. If no such pair exists, we return -1. Given an array A and integer K, we need to find the maximum sum S where S = A[i] + A[j], i < j, and S < K. Algorithm Steps Here's the approach to solve this problem ? Initialize result as -1 If array has only one element, return -1 (need at least 2 elements) Use nested loops ...

Read More
Showing 6701–6710 of 8,549 articles
« Prev 1 669 670 671 672 673 855 Next »
Advertisements