We have seen how spring boot helps us quickly put together RESTful web-services and web applications. When an application or service works as intended it leads to happy users and productivity. However, there are times when software may not be able to fulfill its functions – due to errors or exceptions. The causes of errors […]
Tag: core java
Write to file in Java
This tutorial would be focusing on different ways of write to file using Java. Stream is a general mechanism of I/O in java. Stream provides sequential access of data. An input stream can be used by an application to read data. An output stream to write data. A file, network connection, arrays can act as […]
Compound Assignment Operators in Java
Compound assignment operators are the operators which are syntactically compact. It provides a syntax that is shorter than the normal ones and we use it for allocating the result to any variable for any bitwise or arithmetic calculation. Basically, before allocating the first operand’s result, the operation on the two operands is performed. Types of […]

HashMap in Java
What is Hashing? Hashing is a mechanism to convert one value to another. The process which performs this transformation is called the ‘Hash Function’. The input to the hash function is normally called as the ‘Key’ and this could be any value which can be converted to a String. The output or the hashed value […]
Java Tree Data Structure
In this tutorial, we would be creating a Tree data structure in Java. Java does not have a built in tree data structure. Let’s start by creating a tree as shown in the below image. The tree has a Root node and a number of children Java Tree Implementation Building Tree In Java Tree, each […]
Java Regular Expression Interview Questions
In this article, we would be going through the mostly asked Java Regular expression interview questions and answers. Even if you are not a great fan of interview questions, this post may offer you some interesting excercises on Regular Expressions. Java Regular Expression Interview Questions and Answers What are the classes in Java that helps to […]
Java Collection Interview Questions and Answer
Here is a list of Java Collections interview Questions. These are some of the famous questions asked in interviews. Java Collections Interview Questions What is rehashing? When is it done? In the case of a HashMap, there are two important member fields which determine when exactly the resizing of the map needs to be done. […]
Convert a string representing a duration, ie: 300ms, 20s, etc to to nanoseconds
Suppose you have a string in your application that has the duration of time using ns, us ms , s h, d. And you want to convert them all to nano seconds, then utility will of great help to you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.javagists.learn.utils; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ConvertDuration2TimeUnit { private static final Pattern DURATION_STRING_PATTERN = Pattern.compile("^(\d+)\s?(ns|us|ms|s|m|h|d)?$"); public static long convertDurationString2Nanos(String duration) { final Matcher matcher = DURATION_STRING_PATTERN.matcher(duration); if (!matcher.matches()) { throw new IllegalArgumentException("duration string format invalid"); } final long sourceDuration = Long.parseLong(matcher.group(1)); final String sourceUnitString = matcher.group(2); final TimeUnit sourceTimeUnit; if ("ns".equalsIgnoreCase(sourceUnitString)) { sourceTimeUnit = TimeUnit.NANOSECONDS; } else if ("us".equalsIgnoreCase(sourceUnitString)) { sourceTimeUnit = TimeUnit.MICROSECONDS; } else if ("ms".equalsIgnoreCase(sourceUnitString)) { sourceTimeUnit = TimeUnit.MILLISECONDS; } else if ("s".equalsIgnoreCase(sourceUnitString)) { sourceTimeUnit = TimeUnit.SECONDS; } else if ("m".equalsIgnoreCase(sourceUnitString)) { sourceTimeUnit = TimeUnit.MINUTES; } else if ("h".equalsIgnoreCase(sourceUnitString)) { sourceTimeUnit = TimeUnit.HOURS; } else if ("d".equalsIgnoreCase(sourceUnitString)) { sourceTimeUnit = TimeUnit.DAYS; } else { sourceTimeUnit = TimeUnit.MILLISECONDS; } return sourceTimeUnit.toNanos(sourceDuration); } } |
Longest Palindromic Subsequence – Leetcode Java
Given a string s, find the longest palindromic subsequence’s length in s. You may assume that the maximum length of s is 1000. Example 1: Input:
1 |
"bbbab" |
Output:
1 |
4 |
One possible longest palindromic subsequence is “bbbb”. Example 2: Input:
1 |
"cbbd" |
Output:
1 |
2 |
One possible longest palindromic subsequence is “bb”. Java Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javagists.learn.interview.questions; public class LongestPalindromeSubsequence { public int longestPalindromeSubseq(String input) { int[][] dp = new int[input.length()][input.length()]; for (int i = input.length() - 1; i >= 0; i--) { dp[i][i] = 1; for (int j = i + 1; j < input.length(); j++) { if (input.charAt(i) == input.charAt(j)) { dp[i][j] = dp[i + 1][j - 1] + 2; } else { dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]); } } } return dp[0][input.length() - 1]; } public static void main(String[] args) { LongestPalindromeSubsequence longestPalindromeSubsequence = new LongestPalindromeSubsequence(); System.out.println(longestPalindromeSubsequence.longestPalindromeSubseq("bbbaabbgghj")); } } |
You can […]
License Key Formatting Leetcode – Java
Now you are given a string S, which represents a software license key which we would like to format. The string S is composed of alphanumerical characters and dashes. The dashes split the alphanumerical characters within the string into groups. (i.e. if there are M dashes, the string is split into M+1 groups). The dashes […]