Posts

Lambda expression, stream and functional interfaces are introduced in Java 8. Lambda expressions provide a concise way to express instances of single-method interfaces (functional interfaces). A lambda expression is a block of code that gets passed around, like an anonymous method. It is a way to pass behaviour as an argument to a method invocation and to define a method without a name. A stream is a sequence of data. It is a way to write code that is more declarative and less imperative to process collections of objects. A functional interface is an interface that contains one and only one abstract method. It is a way to define a contract for behaviour as an argument to a method invocation. Below code shows creating a custom thread called Printer class by implementing Runnable interface and creating a thread using lambda. Lambdas.java public class Lambdas { public static void main (String[] args) { Thread thread = new Thread( new Printer()) ; thread.start() ; ...

Two Sum Problem

 Given an array of size N and an integer 'target'. Find the indices (i, j) of two numbers such that their sum is equal to target. (i != j). Assume there is only one solution. array = [11, 7,3, 9, 14, 2] and target = 17 Brut force approach, time complexity will be O(n2) as we need to consider all the pairs  (check an element with other elements in the array). But the space complexity remains constant O(1). Optimisation: Instead of checking an element with all other element, we need to find the difference between the target and the element and check the difference value is present in the array. eg 17 - 11 = 6 and we need to check whether 6 is present in the array.  For this a Hashmap is used with index and integer and before adding the element in the Hashmap check the difference value is present in it. public class TwoSumProblem { public static void main (String[] args) { int [] arr = { 11 , 3 , 7 , 9 , 14 , 2 } ; int target = 17 ; int [] ans = n...

Regex

Using  Regex , we can easily match or search for patterns in a text. Before searching for a pattern, we have to specify one using some well-defined syntax. In this problem, you are given a pattern. You have to check whether the syntax of the given pattern is valid. Note : In this problem, a regex is only valid if you can compile it using the  Pattern.compile  method. import java.util.Scanner; import java.util.regex.*; public class Solution { public static void main(String[] args){ Scanner in = new Scanner(System.in); int testCases = Integer.parseInt(in.nextLine()); while (testCases> 0 ){ String pattern = in.nextLine(); try { Pattern.compile(pattern); System.out.println( "Valid" ); } catch (Exception e) { System.out.println( "Invalid" ); } testCases--; } in.close(); } } import java.util.regex.Matche...

Split

Given a string,  , matching the regular expression  [A-Za-z !,?._'@]+ , split the string into  tokens . We define a token to be one or more consecutive English alphabetic letters. Then, print the number of tokens, followed by each token on a new line. Input Format A single string,  . Constraints  is composed of  any  of the following: English alphabetic letters, blank spaces, exclamation points ( ! ), commas ( , ), question marks ( ? ), periods ( . ), underscores ( _ ), apostrophes ( ' ), and at symbols ( @ ). Output Format On the first line, print an integer,  , denoting the number of tokens in string   (they  do not  need to be unique). Next, print each of the   tokens on a new line in the same order as they appear in input string  . import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s =...