Found 1862 Articles for Data Structure

Difference between concatenation of strings using (str += s) and (str = str + s)

Shubham Vora
Updated on 24-Aug-2023 16:40:30

77 Views

In this tutorial, we will learn the difference between the concatenation of string using the ‘+=’ or ‘+’ operator. Both operators are used to merge strings, but we learn some differences below with examples. What is Addition Assignment (+=) operator? The addition assignment (+=) operator concatenates the two strings. It takes two operands. The left operand is the original string, and the right operand is a string we need to concatenate with the original string. Generally, we use the ‘+=’ operator when we require to concatenate only two strings. Syntax Users can follow the syntax below to use the addition ... Read More

Decrypt the encoded string with help of Matrix as per given encryption decryption technique

Shubham Vora
Updated on 24-Aug-2023 16:35:44

97 Views

In this problem, we need to decrypt the given ciphertext by traversing the matrix diagonally. We can solve the problem by traversing the matrix diagonally. Also, we require to traverse only the upper part of the matrix diagonally to get the decrypted string. Problem statement – We have given an encrypted string of length N and row counts. We need to put the string in the matrix in a row-wise manner. After that, we need to traverse the matrix diagonally, starting from the [0, 0] index to decrypt the string. Sample examples Input str = "TRSI_ _ _UIPN _ ... Read More

Count levels in a Binary Tree consisting of nodes valued 1 grouped together

Prabhdeep Singh
Updated on 24-Aug-2023 15:47:42

102 Views

A binary tree is a tree where each node has a maximum of two children. We are given a binary tree that consists of only 0 and 1 as the node values. We have to find the number of levels of the binary tree that consists of at least one 1 and all the ones of that level must be present consecutively. Example Let's understand with the help of an example − Input 0 / \ 1 0 / ... Read More

Minimize cost to convert all characters of a binary string to 0s

Prabhdeep Singh
Updated on 24-Aug-2023 14:29:16

72 Views

A binary string is a string that only contains the binary numbers in it. In this problem, we are given a binary string, an array that represents the last index up to which we can flip the ones after starting from the ith index which will cost and cost for each index is given in another cost array. We have to perform some number of operations on the string to make the string completely zero. Example Let's understand the problem with the help of an example − Input string str = "101011" int arr[] = {1, 2, 2, 4, 5, ... Read More

Count of 3 sized Strings of all same or different characters using total X 0s, Y 1s and Z 2s

Shubham Vora
Updated on 24-Aug-2023 16:34:30

43 Views

In this problem, we will count the number of strings we can create using the given frequencies such that the string contains same or different characters. We have four options to create a string of length 3 using the 0, 1, and 2 characters. The first string is 012, 000, 111, and 222. So, we need to count the total number of such strings to get the answer. Problem statement – We have given three integer values: X, Y, and Z. The X represents the frequency of the ‘0’, Y represents the frequency of the ‘1’, and Z represents the ... Read More

Convert characters of string1 to characters present in string2 by incrementing or decrementing lexicographically

Shubham Vora
Updated on 24-Aug-2023 16:31:03

52 Views

In this problem, programmers require to make all characters of str1 equal to any character of str2 by performing the increment or decrement operation. Also, we can increment or decrement rotationally. It means ‘z’ + 1 ==‘a’ and ‘a’ – 1 == ‘z’. We can solve the problem by finding the minimum cost of making the str1 string’s character equal to any character of the string str2. For each character, we can find the minimum required operations and sum them all. Problem statement – We have given two strings named str1 and str2. The size of the str1 is ... Read More

Maximize product of array by replacing array elements with its sum or product with element from another array

Prabhdeep Singh
Updated on 24-Aug-2023 14:26:07

102 Views

We are given two arrays of the same length and we have to apply some operations to get the product of the first array with all elements maximum. The operations are to multiply or add any element of the second array only once to any element of the first array only once. We can add or multiply two different elements from the second array element to a single first array element. After all operations, we have to take the product of all the elements of the first array and return that. Example Let's understand the problem with the help of ... Read More

C++ Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String

Shubham Vora
Updated on 24-Aug-2023 16:29:34

78 Views

In this problem, we need to find the maximum consecutive zeros at the start and end of string rotation. We can follow two approaches to solve the problem. The first approach is to find all rotations of the given string and count start and end zeros. The second approach is to count the maximum consecutive zeros in the string and get the answer. Problem statement – We have given the binary string named str of size equal to ‘len’. We need to count the maximum number of consecutive zeros at the start and end of any rotation of the string. ... Read More

C++ Program for Queries for rotation and Kth character of the given string in constant time

Shubham Vora
Updated on 24-Aug-2023 16:22:24

64 Views

In this problem, we need to perform the given queries on the string. We can solve the problem by making the rotations of the string differently and accessing the required characters using their index. Problem statement – We have given string str of length N and array ‘que’ of size M containing queries. We need to perform the queries given in the array according to the below conditions. (1, x) – Make x left rotations of the string. (2, x) – Show the xth character in the output. Sample examples Input que[][2] = {{1, 2}, {2, 1}, ... Read More

Difference between Queue and Deque in C++

Prabhdeep Singh
Updated on 24-Aug-2023 14:24:13

705 Views

Queue and Deque both are linear data structures that are defined in STL of C++ programming language. Queue works on the principle of the first in first out, the element added to the queue first will be removed first, on the other hand, deque has properties to add an element either at the first index or last index, and similarly, any one of them can be removed. We will see the code of both data structures to get the exact differences. Basics of Queue As we have seen above, the queue is based on the concept of the first in ... Read More

Advertisements