# Problem Solving

## Validate Palindrome String in java

Write a Java program to validate palindrome String. Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “Red rum, sir, is murder” is a palindrome, while “Programcreek is awesome” is not. Note: Have you consider that the string might be empty?  we define empty string as …

## Java program to Validate Parentheses

Write a Java program to validate parentheses. Given a string containing just the characters ’(’, ’)’, ’’, ’’, ’[’ and ’]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]” are all valid but “(]” and “([)]” are not. Java program to Validate Parentheses public static …

## Longest Consecutive array Sequence in Java

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, given [100, 4, 200, 1, 3, 2], the longest consecutive elements sequence should be [1, 2, 3, 4]. Its length is 4. Your algorithm should run in O(n) complexity. Because it requires O(n) complexity, we can not solve …

## Java Code to print Date and Time for a specific country

Write a Java program to print the current time in the different countries, In this java program example, we will see how to print the current time in the different countries. Sample Input Asia/Aden Sample Output current time is:09:03:19.232315600 Algorithm to print current time in the different countries Take the name of the country with …

## Write a Java program to print the first and last of given month

Write a Java program to print the first and last date of a given month. In this Java program example, we will see how to print the first and last date of a month.  Sample Input Enter the current date of the month for example 17/03/2022 Sample Output First date: 01/03/2022 Last date: 31/03/2022 Algorithm …

## Find two numbers to add up to a specific target number in Java

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that you returned answers (both index1 and index2) are not …

## Solve Word Break Problem in Java

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = “leetcode”, dict = [“leet”, “code”]. Return true because “leetcode” can be segmented as “leet code”. Solve Word Break Problem in Java Solution 1: public …

## Merge collection of intervals in Java

Given a collection of intervals, merge all overlapping intervals. For Example: Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. The key to solving this problem is defining a Comparator first to sort the ArrayList of Intervals. And then merge some intervals. The take-away message from this problem is utilizing the advantage of sorted list/array class Interval { int start; …

## Find Median of Two Sorted Arrays in Java

There are two sorted arrays A and B of sizes m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). This problem can be converted to the problem of finding kth element, k is (A’s length+ B’ Length)/2. If any of the two arrays …

## Find the shortest chain in Java (Find Ladder Length)

Given two words (start and end), and a dictionary, find the length of the shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start = “hit” end = “cog” dict = [“hot”,”dot”,”dog”,”lot”,”log”] As one shortest …