Program to find length of longest repeating substring in a string in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:39:20

3K+ Views

A repeating substring is a substring that occurs at least twice in a string. In this tutorial, we'll find the length of the longest repeating substring using Python's suffix array approach. So, if the input is like s = "abdgoalputabdtypeabd", then the output will be 3, because the longest substring that occurs more than once is "abd". Algorithm Overview To solve this problem, we will follow these steps − Generate all suffixes of the string Sort the suffixes lexicographically Find the longest common prefix between adjacent suffixes Return the maximum length found Helper ... Read More

Program to find length longest prefix sequence of a word array in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:38:53

293 Views

Suppose we have a list of words called w, with lowercase strings. We have to find the length of the longest sequence of w where each previous word is the prefix of the next word and the next word has just one new character appended. So, if the input is like w = ["pqr", "pq", "m", "mn", "pqrs"], then the output will be 3 because we can get the sequence: ["pq", "pqr", "pqrs"], whose length is 3. Algorithm To solve this, we will follow these steps − Sort the list w ... Read More

Program to find length of longest consecutively increasing substring in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:38:37

308 Views

Given a lowercase string containing English letters and "?" symbols, we need to find the length of the longest consecutively increasing substring that starts with letter "a". For each "?" we can either remove it or replace it with any lowercase letter. For example, if the input is s = "vta???defke", we can transform it into "vtabcdefke" where "abcdef" is the longest consecutively increasing substring starting with "a", giving us a length of 6. Algorithm We'll use a greedy approach to track the current sequence length and question marks ? maxlen − Maximum length found ... Read More

Program to find length of longest consecutive sublist with unique elements in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:38:15

445 Views

Suppose we have a list of numbers called nums, where all elements are unique. We have to find the length of the longest sublist that contains consecutive elements. So, if the input is like nums = [3, 6, 7, 5, 4, 9], then the output will be 5, because the sublist [3, 6, 7, 5, 4] contains all consecutive elements from 3 to 7. Algorithm Approach To solve this, we will follow these steps − Initialize ret := 0 to store the maximum length For each starting ... Read More

Program to find longest consecutive run of 1s in binary form of n in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:37:56

801 Views

Suppose we have a non-negative value n, we have to find the length of the longest consecutive run of 1s in its binary representation. So, if the input is like n = 1469, then the output will be 4, because binary representation of 1469 is "10110111101", so there are four consecutive 1s. Approach To solve this, we will follow these steps ? count := 0 while n is not same as 0, do n := n AND (n after shifting one bit to the left) count := count + 1 return count How It Works The algorithm uses bit manipulation where n & (n

Program to find length of longest substring with 1s in a binary string after one 0-flip in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:37:35

726 Views

Suppose we have a binary string s. We are allowed to flip at most one "0" to "1", we have to find the length of the longest contiguous substring of 1s. So, if the input is like s = "1010110001", then the output will be 4, as if we flip the zero present at index 3, then we get the string "1011110001", here length of the longest substring of 1s is 4. Algorithm To solve this, we will follow these steps ? n := size of s ans := ... Read More

Program to find largest size to truncate logs to store them completely in database in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:37:18

174 Views

Suppose we have a list of numbers called logs and another value limit. Each element in logs[i] represents the size of logs generated by the i-th user. The limit represents the total size of logs we can store in our database. We need to find the largest x such that if we truncate every log in logs to be at most size x, the sum of the truncated log sizes is at most limit. If no log needs to be truncated, then simply return the largest log size. So, if the input is like logs = [500, 200, 10000, ... Read More

Program to find minimum length of first split of an array with smaller elements than other list in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:36:57

251 Views

Suppose we have a list of numbers nums, we want to split the list into two parts part1 and part2 such that every element in part1 is less than or equal to every element in part2. We have to find the smallest length of part1 that is possible (not 0 length). So, if the input is like nums = [3, 1, 2, 5, 4], then the output will be 3, because we can split the list like part1 = [3, 1, 2] and part2 = [5, 4]. Algorithm Approach To solve this problem, we follow these steps ... Read More

Python Pandas - Extract the Number of days for each element from TimeDeltaIndex

AmitDiwan
Updated on 26-Mar-2026 17:36:38

488 Views

To extract the number of days for each element from a TimedeltaIndex object, use the TimedeltaIndex.days property. This property returns an Int64Index containing only the days component from each timedelta. Creating a TimedeltaIndex First, import pandas and create a TimedeltaIndex with various time duration formats − import pandas as pd # Create a TimeDeltaIndex object with different time formats tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999', ... Read More

Program to count number of on lights flipped by n people in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:36:16

297 Views

Suppose we have n toggle switches in a room and n people present in that room. They flip switches according to a specific pattern: Person 1 comes and flips all switches (1, 2, 3, 4, ...) Person 2 comes and flips switches that are multiples of 2 (2, 4, 6, 8, ...) Person i comes and flips switches that are multiples of i We need to find how many switches will be in the ON position after all n people have finished flipping. Understanding the Problem ... Read More

Advertisements