Suppose we have one non-empty string s and a dictionary wordDict. That is containing a list of non-empty words, determine when s can be segmented into a space-separated sequence of one or more dictionary words. We have to follow some rules −The same word in the dictionary may be reused multiple numbers of times in the segmentation.We can assume that the dictionary does not contain duplicate words.Suppose the string s = “applepenapple”, and word dictionary is like [“apple”, “pen”], then the output will be true because the string s can be segmented as “apple pen apple”.Let us see the steps ... Read More
Suppose there is a circle, and there are n gas stations on the circle. We have two sets of data like −The amount of gas that every gas stations hasDistance from one gas stations to another.Calculate the first point, from where a car will be able to complete the circle. Assume for 1 unit of gas, the car can go 1 unit of distance. Suppose there are four gas stations, and the amount of gas, and distance from the next gas stations is as like [(4, 6), (6, 5), (7, 3), (4, 5)], the first point from where car can ... Read More
Suppose we have one input string, a partitioning of that string is palindrome partitioning, when every substring of the partition is a palindrome. In this section, we have to find the minimum cuts are needed to palindrome partitioning the given string. So if the string is like “ababbbabbababa” Minimum cut to partition as palindrome. Here 3 cuts are needed. The palindromes are: a | babbbab | b | ababaTo solve this, we will follow these steps −n := length of strdefine cut matrix and pal matrix each of order n x nfor i := 0 to n, dopal[i, i] := ... Read More
Suppose we have a binary tree. We have to traverse this tree using the level order traversal scheme. So if the tree is likeThe traversal sequence will be like − [10, 5, 16, 8, 15, 20, 23]To solve this, we will follow these steps −define queue que to store nodesinsert root into the que.while que is not empty, doitem := item present at front position of queueprint the value of itemif left of the item is not null, then insert left of item into queif right of the item is not null, then insert right of item into quedelete front ... Read More
Suppose we have a binary tree. We have to traverse this tree using the inorder traversal scheme without using recursion. So if the tree is likeThen the traversal will be [2, 5, 7, 10, 15, 20]To solve this, we will follow these steps −Create two array res and stack, set curr := rootRun one infinite loopwhile current is not nullpush curr into a stack, and set curr := left of currwhen the length of stack = 0, then return resnode := popped element from the stackinsert a value of node into rescurr := right of currExampleLet us see the following ... Read More
Suppose we have a message containing letters from A to Z is being encoded to numbers using the following mapping − 'A' → 1, 'B' → 2 ... 'Z' → 26. So if we have one non-empty string, containing only digits, then we have to find, in how many ways that can be decoded. So if the string is like “12”, then that can be made from “AB”, or “L”, so there are two possible ways. So the answer will be 2.To solve this, we will follow these steps −We will solve this using dynamic programming.n := length of sdp ... Read More
As we know that the gray code is a binary numeral system where two successive values differ in only one bit. Suppose we have a non-negative integer n representing the total number of bits in the code. We have to print the sequence of gray code. A gray code sequence must begin with 0. So if the input is 2, then the result will be [0, 1, 3, 2], this is because gray of 0 is 00, gray of 1 is 01, gray of 2 is 11, and gray of 3 is 10.To solve this, we will follow these steps ... Read More
Suppose we have a linked list and a value x. We have to make partitions. The partition it such that all nodes less than x comes before the nodes that are greater than or equal to x. We should preserve the original relative order of the nodes in each of these two partitions. So if the list is like [1, 4, 3, 2, 5, 2] and x = 3, then the output will be [1, 2, 2, 4, 3, 5]To solve this, we will follow these steps −Make dummy nodes d1 and d2, and initialize them with -1, create two ... Read More
JShell is an interactive command-line tool used to implement simple statements like expressions, classes, methods, fields, interfaces, and etc. String class is part of the built-in java.lang package and provides several methods for common text processing.1) String Utility: String provides several built-in utility methods. The methods like indexOf(), lastIndexOf(), startsWith(), endsWith(), isEmpty(), equals(), equalsIgnoreCase() are that part of string utility.In the below code snippet, we have implemented the string utility methods in the JShell tool.Snippet-1jshell> String str = "JShell is a new feature in Java9"; str ==> "JShell is a new feature in Java9" jshell> str.indexOf("new") $4 ==> 12 ... Read More
Suppose we have two integers n and k. We have to find all possible combinations of k numbers out of 1 ... n. So if n = 4 and k = 2, then the combinations will be [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]To solve this, we will follow these steps −We will use recursive function to solve this. the function solve() is taking n, k, temp array and start. The start is initially 1. This will act likeif size of temp array = k, then insert temp into res array, and returnfor i := ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP