Found 1860 Articles for Data Structure

Python Program for Generating Lyndon Words of Length n

Shubham Vora
Updated on 14-Aug-2023 13:27:24

153 Views

In this problem, we will find all Lyndon words using the array's alphanumeric characters. Before we start, let’s understand the definition of the Lyndon word. All words are Lyndon words which are strictly lexicographically smaller than all of their rotations. Here are examples of Lyndon words. ab − The ‘ab’ is strictly lexicographically smaller than all of its permutations which is ‘ba’. 89 − The rotations of ‘89’ is ‘98’, which is strictly lexicographically larger than ‘89’. abc − The rotations of ‘abc’ are ‘bca’ and ‘cab’, which are strictly greater than ‘abc’. Here ... Read More

PHP Program for Minimum Rotations Required to get the Same String

Shubham Vora
Updated on 14-Aug-2023 13:26:07

126 Views

In this problem, we need to achieve the same string by performing the rotations of the string and need to count rotations. There can be different ways to solve the problem, but here we will learn some best approaches in PHP. The first approach uses the rotational substring, and another approach divides the string into two parts from the particular index. Problem statement − We have a string str containing n characters. We have to find the minimum rotations of string we should perform to achieve the same string again. Sample examples Input str = "bcbc"; Output ... Read More

Minimize Replacement of Bits to make the Count of 01 Substring Equal to 10 Substring

Shubham Vora
Updated on 14-Aug-2023 13:23:50

193 Views

Problem statement − We have given the binary string of length N. We need to find the minimum number of flipping characters required to get the balanced binary string. Flipping characters means converting 0 to 1 and 1 to 0. If any string contains an equal number of ‘01’ and ‘10’ pairs, we can say that string is a balanced binary string. Sample examples Input str = "001010" Output 0 Explanation − The string contains 2 ‘01’ and ‘10’ pairs. So, we don’t need to perform any flipping operations. Input str = ‘00001’ ... Read More

Javascript Program to Check If a String is Substring of Another

Shubham Vora
Updated on 14-Aug-2023 13:22:10

263 Views

In this problem, we need to ensure whether the string String2 contains string String1 as a substring. We will first see the naïve approach to solving the problem, finding all substrings of String1’s length in String2 with String1. Furthermore, we will use built−in methods like match(), includes(), and indexOf() methods to find the String1 substring in the String2. Problem statement − We have given a string String1 and String2 of different sizes. We need to check whether string String1 is present in string String2 as a substring. Sample examples Input string1 = "tutor"; string2 = "pointtutorial"; Output ... Read More

Java Program to Recursively Remove All Adjacent Duplicates

Shriansh Kumar
Updated on 16-Aug-2024 08:06:38

748 Views

The problem statement states that we have given a String str of length N (where N is an integer) containing alphanumeric characters. We need to recursively remove all adjacent duplicate characters so that the resultant string does not contain any adjacent duplicate characters. We can use a recursive or iterative approach to solve the problem. Here, we first remove the adjacent duplicate elements from the left part of the string. After that, we recursively remove the adjacent duplicates from the right part of the string. Example Scenario 1: Input: str1 = "tuttor"; Output: res = tuor The adjacent duplicate ... Read More

Java Program for Minimum Rotations Required to Get the same String

Alshifa Hasnain
Updated on 17-Dec-2024 03:33:40

354 Views

In this article, we will learn to count the total number of minimum required rotations to get the original string in Java. We can solve the problem by getting the rotations substrings of the original string or concatenating the original string to itself.  Problem statement We have given string str of length N. The task is to find the total number of minimum rotations we need to perform to get the original string. Input 1 str = "bcdbcd"; Output 1 3 Explanation − We need to make 3 rotations to get the original string. ... Read More

Java program for minimum move to end operations to make all strings equal

Shubham Vora
Updated on 14-Nov-2024 17:39:40

200 Views

In this article, we will learn how to solve a problem where we are given an array of strings, and we need to make all strings equal by rotating them. This can be done by performing left rotations on the strings. We need to count the minimum number of operations required to do this. If it is impossible to make the strings equal, the output should be -1. Problem statement We have given an array containing n strings. All strings are permutations of each other. We need to count the total number of minimum operations required to make all strings ... Read More

Generate String after Adding Spaces at Specific Positions in a given String

Shubham Vora
Updated on 14-Aug-2023 13:03:58

304 Views

In this problem, we need to add spaces before the given indexes in the string. We can use two different approaches to solve the problem. The first approach moves the characters of the given string to add space at a particular index, and the second approach replaces the characters of the pre−initialize string with spaces. Problem statement − We have given string str of length N containing the alphanumeric characters. Also, we have given the spaceList array containing M positive integers representing the string index. We need to app space to the string before each index that exists in the ... Read More

Find all Palindrome Strings in given Array of Strings

Shubham Vora
Updated on 14-Aug-2023 13:02:13

705 Views

We need to find all palindrome strings from the given array in this problem. The string is a palindrome if we can read it the same from the start and end. We can use two approaches to check whether the string is a palindrome. In the first approach, we reverse the string and compare it with the original string, and in the second approach, we keep comparing the string characters from start to last. Problem statement − We have given an array containing the N strings. We need to print all the strings of the array, which are palindrome. If ... Read More

Decode a String Recursively Encoded as Count followed by Substring

Shubham Vora
Updated on 14-Aug-2023 13:00:40

365 Views

In this problem, we need to decode the given string by repeatedly adding the total count number of times. We can have three different approaches to solving the problem, and we can use two stacks or one stack to solve the problem. Also, we can solve the problem without using the two stacks. Problem statement − We have given a string str containing the opening and closing brackets, alphabetical and numeric characters. We need to decode the string recursively. Here are the patterns or rules to decode the string. [chars] − The ‘chars’ should appear count times in ... Read More

Advertisements