Java program to find longest substring of given string

In this article, we will see java programs to find the longest substring of a string without repeating characters. For example, longest substring of “hello” without repeating characters will be “hel”. So, length of longest sub-string will be 3. import java.util.HashSet; public class Example {   public static void main(String[] args) {     String s = "hello"; […]

Java program to check if a String is rotated version of another String

In the following Java program, we will check if one String can be obtained by rotating characters of another String. For example, following Strings can be obtained by rotating letters of word “hello”: elloh llohe lohel Here is the Java code for the same : package com.topjavatutorial; public class Demo {   public static void main(String[] […]

Java program to find duplicate characters in a string

In this program, we will check for duplicate characters in a String and count the number of times each character is repeated using Java. package com.topjavatutorial; import java.util.HashMap; import java.util.Map; public class Demo {   public static void main(String[] args) {     String str = "topjavatutorial";     int count = 0;     char c;     Map<Character, Integer> map = new […]

8 Different Star(*) Pattern Programs in Java

In this article, we will learn, different star pattern programs in Java.   Star Pattern Example 1: ******** ******* ****** ***** **** *** ** * package com.topjavatutorial; public class JavaStarPattern {   public static void main(String[] args) {     for (int row = 8; row >= 1; –row) {       for (int col = 1; col <= row; […]

How to find difference between two Dates in Java

We can calculate difference between two dates in milliseconds using date1.getTime() – date2.getTime(). However, if you need the difference in the days, months or years, following utility methods might be of help :   Calculate difference in days between two Dates public static int getDayDifference(Date date1, Date date2) {   long differenceInMillis = date1.getTime() – date2.getTime(); […]