Found 1862 Articles for Data Structure

Check whether a Sentence is Tautogram or Not

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

49 Views

In this problem, we need to check whether the given sentence is tautogramic. We can say any sentence is tautogramic if all words have the same starting character. We will learn two approaches to solving the problem. The logic to solve the problem is to check all words' first characters. If any word has a mismatched first character, we can say the sentence is not tautogramic. Problem statement – We have a string containing the N characters. We need to check whether the given string is tautogramic or not. Note – The tautogramic contains all words starting with the same ... Read More

Distance of each node of a Binary Tree from the root node using BFS

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

83 Views

Binary Tree is a non-linear data structure. It has a maximum of two children and each node contain three things that are data value, left and right pointer. The top node is called the root node and the last node which does contain any children is called the leave node. In this problem, we have given a binary tree. It has N nodes in the range from 1 to N (both inclusive) and our task is to find the distance of each node of a binary tree from the root node using BFS. Examples Let's see examples with explanations below ... Read More

Find the Largest and smallest number in an Array containing small as well as large numbers

Prabhdeep Singh
Updated on 24-Aug-2023 14:19:04

85 Views

We are given an array of strings and each string represents a number that could be more than the range of the maximum integer limit. We have to find the largest and the smallest element from the given array. We cannot use the simple less than or greater than operators to check which string is greater as it won't works find with the string so we will make our own comparison function. Example Let's understand the problem with the help of an example − Input string arr[] = {"2", "3", "12", "23", "22", "0", "7"} Output The smallest element ... Read More

Longest Substring containing C2, starting with C1 and ending with C3

Prabhdeep Singh
Updated on 24-Aug-2023 14:17:12

38 Views

A substring is a string that can be achieved from the given string by removing some characters from the beginning and end of the string (possibly none or all). We are given a string and three characters and have to find the longest substring that contains all the three given characters in the sequence of c1, c2, and c3 starting with c1 and ending with c3. Also, the given characters may be the same but we string must contain different characters for each of them. Input string str = "abacdeab" char c1 = a char c2 = b char ... Read More

Count of N length Strings having S as a Subsequence

Prabhdeep Singh
Updated on 24-Aug-2023 14:15:40

84 Views

We are given a string of length S and given an another number n which represents the length of the strings which might contains S as the subsequence. We have to find the number of unique strings of length N which contains S as the subsequence, where a subsequence is the set of characters from the given string which may be all the characters or not and they not need to be continuous. Sample Examples Input string str = "xyz" int n = 3 Output 1 Explanation There is only one string of length 3 that contains the ... Read More

Count of Subsequences of given string X in between strings Y and Z

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

76 Views

A subsequence is a string that can be achieved from another string by removing some (possibly none or all) from a given string that may not be continuous. We are given a string and have to find the number of subsequences that are greater than equal to the given string Y and less than equal to another given string Z. We will use dynamic programming to solve the problem as brute force will take exponential time. Brute Force Approach The brute forces approach is to find all the subsequences of the given string X and then check if they are ... Read More

Longest Substring with at most X 0s and Y 1s of given String

Prabhdeep Singh
Updated on 24-Aug-2023 14:11:39

141 Views

A substring is the continuous sequence of the character from the given string which can be achieved by removing some character from the front and the end of the substring (possibly all or none). We are given a binary string and we have to find the length of the longest substring that contains at most X number zeros and Y number of ones where X and Y are given inputs. Sample Examples Input  string str = "101011"; int x = 1; int y = 2; Output The length of the longest substring with at most X zeroes and ... Read More

XOR of all substrings of a given Binary String

Prabhdeep Singh
Updated on 24-Aug-2023 14:09:57

171 Views

A binary string is a string that contains only two different types of characters in it which are '0' and '1'. Substrings are strings that are formed by deleting some character from the given string from the beginning and from the ending (possibly zero or all). We are given a string and we have to get all the substrings of it and take XOR of it. XOR is a bitwise operator that gives the result − if both the bits are the same then it will return zero otherwise 1. Input  string str = "10101" Output  XOR of all ... Read More

Minimum cost to delete characters from String A to remove any subsequence as String B

Prabhdeep Singh
Updated on 24-Aug-2023 14:02:39

130 Views

We are given two strings string A and string B along with an array that represents the cost to delete the ith character of the given string A. We need to delete some characters of string A (possibly zero or none) with the minimum cost such that no subsequence of A represents string B. We are going to see three approaches to implementing the code that is the recursive approach; the recursive and memoization approach; and tabulation or iterative dp. Example Let's have a look at the following example − Input string a = "xanxd" string b = ... Read More

Transform string A into B by deleting characters from ends and reinserting at any position

Prabhdeep Singh
Updated on 24-Aug-2023 13:56:43

50 Views

Anagram of a string means a string contains exact same characters as another string with the order of characters may vary from the original string, so we called that both strings are anagram of each other. Here we have given the two strings first and second which are anagrams of each other. And our task is to minimize the count of operations to make the first string as second string. An operation is that we can delete an character from the begin or end of an first string and reinserting at any position. Sample Example Input  First: "hello", Second: "ohlle" ... Read More

Advertisements