Posts

Java program to return the first non repeated character in a string

Image
This post is about java program to return the first non repeated character in a string. Example: Given a string "hhello", It should return 'e' as the first non repeated character. Logic of the program: Using a linked hash map will solve this program. All the characters[a-z] can be stored in a linked hash map as key and value should be corresponding to be number of occurrence of each character.  To return the first non repeated char, just iterate the linked hash map and return first key having value as 1. Program: package com.techjourney; import java.util.LinkedHashMap; import java.util.Map; /** * Created by Sujith Mohan on 6/4/2015. */ public class FirstNonRepeatedChar { public static char returnFirstNonRepeatedChar(String str) { if (str== null ) throw new NullPointerException(); if (str.equals( "" )) return '\0' ; LinkedHashMap<Character, Integer> map = ...

Java program to check if a string is pangram

Hi everyone, in this blog, I will be writing about a java program to check whether a string is pangram or not. Pangrams are words or sentences containing every letter of alphabet at least once. Most commonly used example of pangram is " A quick brown fox jumps over the lazy dog. " Logic of the program Initialize a string testString as "abcdefghijklmnopqrstuvwxyz" Loop the input string for every character, and replace those character in testString with empty character. If testString is an empty string, then input string is Pangram The program for pangram is as follows: package com.blog.techjourney; public class Pangram { public static Boolean checkForPangram(String str) { int len = str.length(); //test string for testing all characters are present in the given string String testString = "abcdefghijklmnopqrstuvwxyz" ; for ( int i=0;i<len;i++) { char chars =str.charAt(i); //Converting the char to String a...

Difference between "diff" and "sdiff" commands in Unix

Image
Hi everyone, in this blog post, I will be discussing about the difference between "diff" and "sdiff" commands in Unix. This is my first post about Unix. diff command diff command will compare two files and prints the difference between the 2 files. The format of diff command is diff file1 file2 . sdiff command sdiff command will compare two files side by side. The format of sdiff command is sdiff file1 file2. The contents of files are displayed side by side with a marker denoting the difference between each files. Some of the important markers used in sdiff are as follows: | - denotes the 2 files differ in the line completely. < - only the first file contains the line. > - only the second file contains the line. The better way to depict the difference is by showing examples. Here, I am comparing 2 files which are named as " ipl_teams_2008.txt " and " ipl_teams_2015.txt ". Let's see the contents of the files. ...

Java program to check if two elements of an array adds to the given sum

Hi everyone, in this blog post, I will be discussing about a famous java program, which is to check if two elements of an array adds to the given sum. Problem Given an array with elements and a sum. Check whether any 2 elements of the array adds to the given sum. Return true if the sum is present, else return false. Example Given an array {1,2,4}. The array should return true for the sum 5 and false for 4. Solution The basic solution which comes to everyone's mind would be to check each element with each other elements for the given sum. But the total time complexity in doing that will be O(n^2). We will approach the problem in a much easier way. The logic for that is as follows: Sort the given array in ascending order. Create two variables, one which tracks from the first position of array ( pos_first ) and other which tracks from the last position of the array ( pos_last ). While pos_first is less than pos_last , do the below steps. Add the elements corr...

Java program to change the elements of the array with the product of remaining elements

Hi Everyone, In this blog post I will be discussing about an interesting program, which is to change the " elements of the array with the product of remaining elements ". This program is really interesting as well as tricky. If an array contains 0, the product of all the elements in the array will be 0. For eg: If an array is [0,1,2], after performing the algorithm, it will be [2,0,0]. Logic The logic of the program is as follows: Initialize an integer variable product = 1. (This variable is used to store the product of all elements of array) Initialize an integer variable countOfZeros =0 Find the product of all the non-zero elements in the array as well as count the number of zero's in the array. If countOfZeros is greater than or equal to 2, then change all the elements of array to 0. If countOfZeros is 1, then change all non zero elements to 0 and 0 to product . If countOfZeros is 0, then change all elements to product /(element). IDE used - Intel...

Java program to get the Next Greater Element in an array

Hi Everyone, it's been a long time since I have made a post. So here goes one. In this blog post, I will be writing about a Java program to get the next greater element in an array. Expected output : For every element in the array, print the number which is the next greater to the element and which occurs after the element in position. If greater element is not found print -1. Logic of the program : Assign an element next greater value, " ngv "as -1. For every element " i " in the array, loop from the next element till last element. In the loop, if an element is greater than " i " and " ngv " is -1 , assign " ngv " as the element. In the loop, if an element is greater than " i " and " ngv " is not -1, check whether " ngv " greater than that element. If so, assign " ngv " as the element. Return " ngv ". The java program is as follows: package  com.blog.techjourney;...

Java program to access an URL

Hi Everyone, in this blog post I will be writing a java program to access an URL. The Java program to connect to an URL is as follows: package com.blog.techjourney; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class AccessURL { public static void main(String[] args) { String url = "http://sujithforu.blogspot.com/" ; String fakeurl = "http://thisisanincorrecttypeofurl.com/" ; System.out.println( " Connecting to existing url - " +url); getURLResponse(url); System.out.println(); System.out.println( " Connecting to fake url - " +fakeurl); getURLResponse(fakeurl); } public static void getURLResponse(String str) { URL url; URLConnection urlConnection; HttpURLConnection httpConnection = null ; try { url = new URL(str); urlConnection = url.openConnection(); urlConnection.co...