Programming Articles - Page 1972 of 3363

Replace the Substring for Balanced String in C++

Arnab Chakraborty
Updated on 02-May-2020 11:27:44

512 Views

Suppose we have a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string will be balanced if each of its characters appears n/4 times where n is the length of the string. Find the minimum length of the substring that can be replaced with any other string of the same length to make the original string is balanced. So if s = “QQWE”, then output will be 1. This is because we can replace Q to R, so that “RQWE”, that is balanced.Return 0 if the string is already balanced.To solve this, we will follow ... Read More

How to get system properties in JShell in Java 9?

raja
Updated on 02-May-2020 11:09:29

506 Views

JShell is a REPL (Read-Evaluate-Print-Loop) tool used to execute simple statements, evaluates it, and displays the result without a main() method. We can start it by simply type "jshell" in command-line prompt.We need to get the system properties by using System.getProperty() and System.getProperties() methods.In the below code snippet, we can able to display the system properties in the JShell tool by using static method property() of System class.Snippet-1jshell> System.getProperty("java.class.path") $1 ==> "C:\Program Files\Java\jdk-9.0.4\lib;C:\json-jars\json.jar;.;C:\json-jars\json-simple.jar;.;C:\json-jars\gson.jar;.;C:\json-jars\commons-io.jar;.;C:\json-jars\jackson-core.jar;.;C:\json-jars\jackson-databind.jar;.;C:\json-jars\jackson-annotations.jar;.;C:\json jars\flexjson.jar;.;C:\json-jars\jackson-dataformat-xml.jar;.;C:\json-jars\stax2-api.jar;.;C:\json-jars\jackson-dataformat-csv.jar;.;C:\json-jars\javax.json.jar;.;C:\json jars\javax.json-api.jar;.;C:\json-jars\jackson-module-jsonSchema.jar;.;C:\json-jars\json-lib.jar;.;C:\json-jars\commons-lang.jar;.;C:\json-jars\commons-logging.jar;.;"In the below code snippet, we have to use the “properties” object that extends Hashtable. So all properties can be listed as key/value pairs in the JShell tool by using ... Read More

K-Concatenation Maximum Sum in C++

Arnab Chakraborty
Updated on 02-May-2020 09:28:30

335 Views

Suppose we have an integer array arr and one integer k, we have to change the array by repeating it k times. So if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].Now we have to find the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0. As the answer may be very large, find the answer modulo 10^9 + 7.So if the input is like [1, -2, 1] and k = 5, ... Read More

How to implement JShell using JavaFX in Java 9?

raja
Updated on 02-May-2020 09:45:07

317 Views

JShell is an interactive tool used to implement sample expressions. We can implement JShell programmatically using JavaFX application then we need to import a few packages in the java program listed belowimport jdk.jshell.JShell; import jdk.jshell.SnippetEvent; import jdk.jshell.VarSnippet;In the below example, implemented a sample Java FX application. We will enter different values in the text field and press the "eval" button. It will display values with corresponding data types in a list.Exampleimport javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.Stage; import java.util.List; import jdk.jshell.JShell; import jdk.jshell.SnippetEvent; import jdk.jshell.VarSnippet; public class JShellFXTest extends Application {    @Override    public void start(Stage primaryStage) ... Read More

Invalid Transactions in C++

Arnab Chakraborty
Updated on 02-May-2020 09:25:30

252 Views

Suppose there are some transactions. A transaction is possibly invalid if −The amount exceeds $1000, or;If it occurs within (and including) 60 minutes of another transaction with the same name in a different city.Here each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction. We have a list of transactions, find a list of transactions that are possibly invalid. So if the input is like ["alice, 20, 800, mtv", "bob, 50, 1200, mtv"], then the answer will be ["bob, 50, 1200, mtv"].To solve this, we will follow these steps ... Read More

As Far from Land as Possible in C++

Arnab Chakraborty
Updated on 02-May-2020 09:21:47

351 Views

Suppose we have one N x N grid containing only values like 0 and 1, where 0 represents water and 1 represents the land, we have to find a water cell such that its distance to the nearest land cell is maximized and return the distance. Here we will use the Manhattan distance − the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|. If no land or water is present in the grid, then return -1.101000101Then the output will be 2, as the cell (1, 1) is as far as possible ... Read More

Camelcase Matching in C++

Arnab Chakraborty
Updated on 02-May-2020 09:17:22

819 Views

Suppose we have a list of queries, and a pattern, we have to return an answer that will be list of booleans, where answer[i] is true if and only if queries[i] matches the pattern. A query word matches a given pattern when we can insert lowercase letters to the pattern word so that it equals the query.So if the input is like ["FooBar", "FooBarTest", "FootBall", "FrameBuffer", "ForceFeedBack"] and pattern = "FB", then the results will be [true, false, true, false, false].To solve this, we will follow these steps −Define a method called insertNode(), this takes head and string scurr := ... Read More

Minimum Swaps To Make Sequences Increasing in C++

Arnab Chakraborty
Updated on 02-May-2020 09:14:29

370 Views

Suppose we have two integer sequences A and B of the same non-zero length. We can swap elements A[i] and B[i]. We have to keep in mind that both elements are in the same index position in their respective sequences. After completing some number of swaps, A and B are both strictly increasing. We have to find the minimum number of swaps to make both sequences strictly increasing.So if the input is like A = [1, 3, 5, 4] and B = [1, 2, 3, 7], then the answer will be 1, if we swap A[3] with B[3], then the ... Read More

All Paths From Source to Target in C++

Arnab Chakraborty
Updated on 02-May-2020 09:11:33

539 Views

Suppose we have a directed, acyclic graph with N nodes. We have to find all possible paths from node 0 to node N-1, and return them in any order. The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.So if the input is like [[1, 2], [3], [3], []], then the output will be [[0, 1, 3], [0, 2, 3]].To solve this, we will follow these steps −Make one 2d array called resDefine a method called solve, this will take ... Read More

Number of Subarrays with Bounded Maximum in C++

Arnab Chakraborty
Updated on 02-May-2020 09:09:25

238 Views

Suppose we have an array A of positive integers, and two positive integers L and R are also given. We have to find the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least L and at most R. So if A = [2, 1, 4, 3] and L = 2 and R = 3, then output will be 3 as there are three sub arrays that meet the requirements. So these are [2], [2, 1], [3].To solve this, we will follow these steps −ret := 0, dp := 0, ... Read More

Advertisements