Monday, June 28, 2021

Supervised Machine Learning

Supervised Machine Learning

Supervised Machine Learning is a subset of Machine Learning where you have the input variable (X) and the output variable (Y) and use an algorithm to learn the mapping function from the input to the output.

It is called supervised learning because it is the process of an algorithm learning from the training data set. It can be thought of as a teacher supervising the learning process.

It is defined by its use of labeled datasets to train algorithms to classify data or predict outcomes accurately. As input data is fed into the model, it adjusts its weights until the model has been fitted appropriately, which occurs as part of the cross-validation process. Supervised learning helps organizations solve a variety of real-world problems at scale, such as classifying spam in a separate folder from your inbox.


Supervised Learning


If we have a look at the supervised learning workflow the following steps are involved,


  • Initially, we have some historical data
  • Then we apply random sampling.
  • In the next step, we split this data into training data set and the testing data set.
  • After this, with the help of supervised machine learning statistical model is created.
  • After this, we use the testing data set for production and testing.
  • Finally, we have the model validation outcome.

If we have a look at the prediction part of any particular supervised learning algorithm, the model is used for generating outcomes from a new data set. Whenever the performance of the model is degraded, the model is retrained or if there are any performance issues, the model is retrained with the help of the new data. This allows the model to learn over time. The algorithm measures its accuracy through the loss function, adjusting until the error has been sufficiently minimized.

Supervised learning can be divided into two types of problems,

Classification

Classification uses an algorithm to accurately assign test data into specific categories. It recognizes specific entities within the dataset and attempts to draw some conclusions on how those entities should be labeled or defined. Common classification algorithms are linear classifiers, support vector machines (SVM), decision trees, k-nearest neighbor, and random forest, which are described in more detail below.

Regression

Regression is used to understand the relationship between dependent and independent variables. It is commonly used to make projections, such as for sales revenue for a given business. Linear regression, logistical regression, and polynomial regression are popular regression algorithms.

 

Supervised Learning
Supervised Learning



Wednesday, June 23, 2021

Jupyter Notenook(Beginner's Guide)

Jupyter Notebook


In the early days, scientists used to have a lab notebook to test progress results and conclusions. Jupyter is a modern-day notebook that allows data scientists to record the complete analysis process. Much in the same way other scientists use a lab notebook.
 

Jupyter Notebook



The Jupyter product was originally developed as a part of the IPython project. The iPython project was used to provide interactive online access to python. IPython is still an active tool and it is still available for use. The name Jupyter itself is derived from the combination of Julia, Python, and R.

It is strongly recommended to install Python and Jupyter using Anaconda distribution, which includes python. Especially, when you don't know much about other commonly used packages for scientific computing as well as for data science.

Although one can also do the same using the pip installation method personally. But, it is suggested to use Anaconda Navigator, which is a desktop graphical user interface included in Anaconda. Now, this allows you to launch applications and easily manage the Conda packages environment. It even works without the need to use command line commands to Anaconda Navigators.

Installation

The easiest way for a beginner to get started with Jupyter Notebooks are by installing Anaconda.

Anaconda is the most widely used Python distribution for data science and comes pre-loaded with all the most popular libraries and tools. Some of the biggest Python libraries included in Anaconda include NumPy, pandas, and Matplotlib, though the full 1000+ list is exhaustive.

Anaconda thus lets you hit the ground running with a fully stocked machine learning and data science workshop without the hassle of managing countless installations or worrying about dependencies and OS-specific installation issues.

To get Anaconda, simply: 

  • Download the latest version of Anaconda.
  • Install Anaconda by following the instructions on the download page and/or in the executable.

 

Once you download the Anaconda Navigator, it looks something like this.

Jupyter Navigator 



Anaconda Navigator has Jupyter notebook, Qt console (which is IPython console), It has Spider IDE which is similar to an R Studio in terms of Python, it has Jupyter Lab, Orange3, Glue, and VS Code.

Jupyter Notebook is fundamentally a JSON file with a number of annotations. It has three main parts which are the metadata, Notebook format, and the list of cells.

Jupyter user interface has a number of components. It's important to know what components you should be using on a daily basis and you should get familiar with them.

Once Jupyter notebook is launched it looks like this.

Jupyter notebook



It creates an online python instance for you to use over the web. We have a Jupyter label on the top left. This acts as a button to go to your home page whenever it is clicked.

There is a dashboard having three tabs displayed. Files, running, and clusters.


jupyter tabs




The file tab shows the list of the current files in the directory.

The running tab presents another screen of the currently running processes and the notebooks.

The cluster tab presents another screen to display the list of clusters available.

In the top right corner of the screen, there are three buttons which are the upload, new, and the refresh button.

python 3

 

The upload button is used to add files to the Notebook space. You may also just drag and drop as you would when handling files.

Similarly, you can drag and drop notebooks into specific folders as well.

The menu with the new in the top further has text file, Folder, terminal, and Python 3 as options.

The text file option is used to add a text file to the current directory.

Jupyter will open a new browser window for you for running a new text editor. The text entered in this file is automatically saved and will be displayed in your notebook files display. The folder option creates a new folder with the name Untitled folder. All the files and folder names are editable. The terminal option allows you to start an IPython session. The notebook option available will be activated when additional notebooks are available in your environment.

The Python 3 option is used to begin Pythons session interactively in your notebook. The interface looks like the following screenshot.

python 3 ide



You have is full file editing capabilities for your scripts including saving as a new file.

You also have a complete working IDE for your python script.

The refresh button is used to update the display. It's not really necessary as a display is reactive to any changes in the underlying file structure.

There is also a check box drop-down menu and a home button.

jupyter notebook


The check box is used to toggle all the checkboxes in the item list. You can select all of these when either move or either delete all of the files selected. You can select all and deselect some of the files as your wish. The drop-down menu presents a list of choices available, which are the folders all notebooks running, and the files to the folder section.


Jupyter Notebook Cells


Cells in Jupyter notebook are of four types − Code, Markdown, raw nbconvert and heading.

 

Code Cells

Contents in this cell are treated as statements in a programming language of the current kernel. The default kernel is Python. So, we can write Python statements in a code cell. When such a cell is run, its result is displayed in an output cell. The output may be text, image, matplotlib plots or HTML tables. Code cells have rich text capability.

 

Markdown Cells

These cells contain text formatted using markdown language. All kinds of formatting features are available like making text bold and italic, displaying ordered or unordered lists, rendering tabular contents, etc. Markdown cells are especially useful to provide documentation to the computational process of the notebook.

 

Raw nbconvert Cells

Contents in raw cells are not evaluated by notebook kernel. When passed through nbconvert, they will be rendered as desired. If you type LatEx in a raw cell, rendering will happen after nbconvert is applied.

  

Heading Cells


Contents in these cells are used to mark the different levels of headings.


jupyter cells




Jupyter Notebook Security


Jupyter Notebooks are created in order to be shared with other users. In many cases over the Internet. However, Jupyter notebook can execute arbitrary code and generate arbitrary code. This can be a problem. If malicious aspects have been placed in the note. The default security mechanism for notebooks includes raw HTML, which is always sanitized and checked for malicious coding.

Another aspect is that you cannot run external Java scripts. Now the cell contents, especially the HTML and the JavaScript are not trusted. It requires user evaluation to continue and the output from any cell is not trusted. HTML or JavaScript is never trusted and clearing the output will cause the notebook to become trusted when save now notebooks can also use a security digest to ensure the correct user is modifying the contents.




Monday, June 21, 2021

Introduction To Machine Learning

Introduction to Machine Learning


We are living in a world of humans and machines. Humans have been evolving and learning from past experience for millions of years, on the other hand, the era of machines and robots have just begun in today's world. These machines and the robots need to be programmed before they actually follow our instructions. But, what if the machine started to learn on their own and this is where machine learning comes into the picture. Machine learning is the core of many futuristic technology advancements in our world.

Today, we can see various examples or implementations of machine learning around us such as Tesla's self-driving car Apple Siri, Sophia AI robot, and many more are there.

 

What exactly does machine learning mean?

Machine learning is an area of artificial intelligence that focuses on the design of a system that can learn and make decisions and predictions based on experience (That is data). Machine Learning enables computers to act and make data-driven decisions rather than being explicitly programmed to perform a certain task. These programs are designed to learn and improve over time when exposed to new data.

AI, Machine Learning, and Deep Learning?

Artificial intelligence is a broader concept of machines being able to carry out tasks in a smarter way. It covers anything which enables the computer to behave like humans do. You can think of a famous Turing Test to determine whether a computer is capable of thinking like a human being or not.

If you are talking to Siri on your phone and you get an answer you're already very close to it. So machine learning is a subset of AI. Machine Learning is based on the idea that we should be able to give machines access to data and let them learn from that data. This means that the machine can not only find the rules for optimal Behaviour but also can adapt to the changes in the world. Many of the algorithms involved have been known for decades now, thanks to the advances in computer science and parallel Computing. They can now scale up to massive data volumes. 

  

Deep Learning is a subset of Machine Learning

It is very similar to machine learning. However, it used to train deep neural networks. So, to achieve better accuracy in those cases where Machine Learning was not performing up to the mark Deep Learning is used.

img


How does Machine Learning work?

Machine Learning algorithms are trained using a labeled or unlabelled training data set to produce a model. New input data is introduced to the machine learning algorithm and it makes a prediction based on the model. This prediction is evaluated for accuracy and if the accuracy is acceptable the machine learning algorithm is deployed.




If the accuracy is not acceptable the machine learning algorithm is trained again, and again with an extended training data set.

This was just a high-level example as there are many more factors and other steps involved in it. We can subcategorize Machine Learning into three different types

  •       Supervised learning
  •       Unsupervised learning
  •       Reinforcement

 

We can see how each of them is used in the field of banking, Healthcare retail, and other domains.

 
Supervised learning


Supervised learning is the subcategory of machine learning that focuses on learning a classification or regression model, which is, learning from labeled training data.

img

A binary classification problem (plus, minus) and two feature variable (x1 and x2)

Or in simple words

Supervised learning is a type of Machine Learning method, where each instance of a training data set is composed of different input attributes and expected output. 

The input attributes of a training data set can be of any kind of data. It can be a pixel of the image. It can be a value of a database row or it can even be an audio frequency histogram.

For each input instance and expected output values, the associated value can be discrete, representing a category, or can be a real or continuous value in either case.

The algorithm learns the input pattern that generates the expected output. Once the algorithm is trained, it can be used to predict the correct output for a never-seen input.

We can see this in the following image.

 

img

We are feeding raw inputs as images of Apple to the algorithm as a part of the algorithm. We have a supervisor who keeps on correcting the machine or who keeps on training the machine. It keeps on telling him that yes, it is an Apple. Or No, it is not an apple thing. This process keeps on repeating until we get a final trained model. Once the model is ready. It can easily predict the correct output of a never-seen input.

For example, we are giving an image of a green apple to the machine and the machine can easily identify it as yes, it is a green apple and it is giving the correct result.

Let's see, another example of supervised learning is,

The image below shows an example of a supervised learning process used to produce a model which is capable of recognizing the ducks in the image.




The training data set is composed of labeled pictures of ducks and not Ducks. The result of the supervised learning process is a predictor model which is capable of associating a label duck, or not a duck, to the new image presented to the model. Once trained successfully and the model achieves a satisfactory level of acceptance, the resulting predictive model can be deployed to the production environment.

Why is this category of Machine Learning named Supervised Learning?

It is called supervised learning because the process of an algorithm learning from the training data set can be thought of as a teacher supervising the learning process.

Some popular Supervised Learning algorithms are,

  •       Linear regression
  •       Random forest
  •       Support Vector machines.

We will discuss these algorithms later,

Now, let's see some of the popular use cases of supervised learning.

We have Crotona or another speech recognition automation in your mobile phone that trains using your voice. Once, it is trained, it starts working based on the training. This is an application of supervised learning.

Suppose, you are telling “Ok! Google call Sam” or you say “Hey! Siri call Sam”. You get an answer and the action is performed automatically. A call is made to Sam. Another example of supervised learning is the weather app based on some of the prior knowledge like when it is sunny the temperature is high. Actually, as we are feeding the data to the machine and telling that whenever it is sunny. The temperature should be higher whenever it is cloudy. The humidity should be higher. So, it's an example of supervised learning.

Another example is biometric attendance where you train the machine and after a couple of inputs of your biometric identity, you put your thumb upon the device. The trained machine automatically validates your future input and can identify you.

Next comes the field of the banking sector in the banking sector supervised learning is used to predict the creditworthiness of a credit card holder by building a machine learning model to look for faulty attributes by providing it with data on delinquent and non-delinquent customers.

Next comes the healthcare sector in the healthcare sector.

It is used to predict the patient's readmission rates by building a regression model by providing data on the patient’s treatment, admissions, and readmissions to show variables that best correlate with readmission. The next example comes from the retail sector and retail sector. Here, It is used to analyze the product. We can build a supervised model to identify frequent itemsets and association rules from the transactional data.


Unsupervised Learning


In contrast to supervised learning, unsupervised learning is a branch of machine learning that is concerned with unlabelled data. Common tasks in unsupervised learning are clustering analysis (assigning group memberships) and dimensionality reduction (compressing data onto a lower-dimensional subspace or manifold). The goal for unsupervised learning is to model the underlying structure or distribution.


Unsupervised learning

In the unsupervised learning approach, the data instances of a training data set do not have an expected output associated with them instead unsupervised learning algorithm detects patterns based on innate characteristics of the input data.

Tasks that apply unsupervised learning are clustering. In clustering, similar data instances are grouped together in order to identify clusters of data. 



 

Unsupervised learning


In the above image can see that initially, we have different varieties of fruits as input. Now, these sets of fruits as input X are given to the model.

Now, what will be the model?

It is trained using an unsupervised learning algorithm. The model will create clusters on the basis of its training. It will group the similar fruits and make their cluster.

Let's take another example

The image below shows an example of the unsupervised learning process. This algorithm processes an unlabelled training data set based on the characteristics.


Unsupervised learning


It groups the picture into three different clusters of data despite the ability of grouping similar data into clusters.

The algorithm is not capable to add labels to the row. The algorithm only knows which data instances are similar, but it cannot identify the meaning of this group. You must be wondering why this category of machine learning is named unsupervised learning.

It is called unsupervised learning because, unlike supervised learning, there is no correct answer and there is no teacher. The algorithms are left on their own to discover and present the interesting structure in the data.

Some of the popular unsupervised learning algorithms are,

  • K-Means Algorithm
  • Apriori Algorithm
  • Hierarchical Clustering Algorithm

 

Few examples of unsupervised learning

Suppose, a friend invites you to his party, where you meet total strangers. Now, you will classify them using unsupervised learning as you don't have any prior knowledge about them and this classification can be done on the basis of gender, age group, dressing, education, qualification, or whatever way you might like. But, why this learning is different from supervised learning. Since you do not use any past or prior knowledge about the people. You kept on classifying them on the go as they kept on coming. You just kept on classifying them.

Another example

Suppose, you are a cricket fan and you have never seen a football match before and by chance you watch a video on the internet. You can easily classify the players on the basis of different criteria, like players wearing the same kind of Jersey are in one class, and the players wearing different kinds of Jersey are in different classes. You can classify them on the basis of their playing style like this guy is an attacker and another one is the defender. So this was also an example of unsupervised learning.

How unsupervised learning is used in the sectors of banking and Healthcare?

In the banking sector, it is used to segment customers by behavioral characteristics by surveying prospects and customers to develop multiple segments using clustering.

In the Healthcare sector, it is used to categorize the MRI data by normal or abnormal ages. It uses deep learning techniques to build a model that learns from different features of images to recognize a different pattern.

In the Retail sector, it is used to recommend the products to customers based on their past purchases. It does this by building a collaborative filtering model based on their past purchases.


Reinforcement Learning


Reinforcement learning is a type of machine learning algorithm which allows software agents and machine to automatically determine the ideal behavior within a specific context to maximize its performance. Reinforcement learning is about the interaction between two elements, the environment, and the learning agent. The learning agent leverages two mechanisms namely exploration and exploitation. When a learning agent acts on a trial and error basis it is termed exploration and when it acts based on the knowledge gained from the environment, it is referred to as exploitation.

 

Reinforcement Learning

The environment rewards the agent for correct actions, which is a reinforcement signal. Leveraging the rewards agent improves its environment knowledge to select the next action.

Reinforcement Learning


In the image above, you can see that the machine is confused about whether it is an apple or it's not an apple. The machine is trained using reinforcement learning, If, it makes a correct decision it gets a reward point for it, and in case of wrong it gets a penalty for that.

Now, the machine can easily identify which one of them is an apple and which one is not.

Typical applications of reinforcement learning involve playing games (chess, Go, Atari video games) and some form of robots, e.g., drones, warehouse robots, and more recently self-driving cars.

Let's see an example here

We can see that we have an agent who has to judge from the environment to find out which of the two is a duck. The first task he did is to observe the environment next it select some action using some policy. If the machine has made a wrong decision by (for example) choosing a bunny as a duck. So the machine will get a penalty for it. For example -50.4 for the wrong answer.

The machine will update its policy and this will continue till the machine gets an optimal policy. From the next time onwards the machine will know that bunny is not a duck.

How reinforcement learning is applied in the field of banking, Healthcare, and Retail sector?

In the banking sector,

Reinforcement learning, for example, can be used to create the next best offer model for a call center by building a predictive model that learns over time as users accept or reject offers made by the sales staff.

 In the healthcare sector, for example, it can be used to allocate the resources to handle different type of cancer cases by building a Markov decision process that learns treatment strategies for each type of cancer case next and the last comes

In the retail sector, for example, it can be used to reduce excess stock with dynamic pricing by building a dynamic pricing model that is just the price based on customer response to the offers.


AI versus Machine learning versus Deep learning


These are the terms that have confused a lot of people.

Artificial intelligence is a broader area in which machine learning and deep learning come. Deep learning is a subset of machine learning and Machine learning is the subset of AI.

How exactly they differ from each other?

The term artificial intelligence was first coined in the year 1956. The concept is pretty old, but it has gained popularity recently.

But why AI has gained its popularity recently?

The reason is earlier we had a very small amount of data the data we had was not enough to predict the correct result but now there's a tremendous increase in the amount of data. Statistics suggest that by 2020 the accumulated volume of data will increase from 4.4 zettabytes to roughly around 44 zettabytes or 44 trillion GBs of data along with such an enormous amount of data.

Now, we have more advanced algorithms and high-end computing power and storage that can deal with such a large amount of data as a result.

What actually AI is?

It's nothing but a technique that enables the machine to act like humans by replicating the behavior and nature with AI. It is possible for machines to learn from experience like humans do. The machines just respond based on new input thereby performing human-like tasks artificial intelligence can be and to accomplish specific tasks by processing a large amount of data and recognizing patterns in them.

You can consider that building an artificial intelligence application is like Building a Church. The first church took generations to finish. So most of the workers were working on it never saw the final outcome. So, those working on it took pride in their craft building bricks and chiseling stone that was going to be placed into the great structure.

So as AI researchers, we should think of ourselves as a humble brick maker’s job. It's just a study of how to build components. Examples of AI are Apple series chess-playing, computer Tesla self-driving car. Many more such examples are based on deep learning and natural language processing.


What is deep learning?


The concept of deep learning is not new, but recently it's getting hype as increased demand. This field is a particular kind of machine learning that is inspired by the functionality of our brain cells called neurons which led to the concept of artificial neural network. It simply takes the data connection between all the artificial neurons and adjusts them according to the data pattern.

More neurons are added as the size of the data is large. It automatically features learning at multiple levels of abstraction. Thereby allowing a system to learn complex function mapping without depending on any specific algorithm.

Let us try and understand Deep Learning with the help of an example, how would you recognize a square from other shapes. The first thing we can do is check whether there are four lines associated with a figure. If yes, we further check that if they are connected and closed again. We finally check whether incident sides are perpendicular to each other and all its sides are equal. If all conditions are satisfied.

Yes, it is a square.

Actually, it is nothing but a nested hierarchy of Concepts. What we did here we took a complex task of identifying a square and broke it into simpler tasks. Deep learning also does the same thing but on a larger scale.

Let's take another example of machine learning, which recognizes an animal. The task of the machine is to recognize whether the given image is of a cat or a dog. What if we were asked to resolve the same issue using the concept of machine learning what we would do first?

We would define the features such as check whether the animal has whiskers or not. The animal has pointed ears or not or whether its tail is straight or curved in short. We will define the facial features and let the system identify which features are more important in classifying a particular animal. When it comes to deep learning it takes this one step ahead.

Deep learning automatically finds the features which are most important for classification. On the other hand in machine learning, we have to manually provide the features. Also, the Deep Learning algorithm needs a large amount of data to understand it perfectly. On the other hand, the machine learning algorithm can easily work with smaller data set.


Machine Learning Vs Deep Learning


High-end machines

Deep learning algorithms are heavily dependent on high-end machines while the machine learning algorithm can work on low-end machines as well. This is because the requirement of a deep learning algorithm includes GPUs which is an integral part of its working the Deep learning algorithm. GPUs can do a large amount of matrix multiplication operations, and these operations can only be efficiently optimized using a GPU.

Feature engineering

Another parameter is feature engineering. Feature engineering is a process of putting the domain knowledge to reduce the complexity of the data. Make patterns more visible to learning algorithms. This process is difficult and expensive in terms of time and expertise in the case of machine learning. Most other features are needed to be identified by an expert and then hand-coded as per the domain requirement and the data type.

For example, the features can be a pixel value, shapes, texture, position, orientation, or anything. The performance of most of the machine learning algorithms depends on how accurately the features are identified and understood. As in the case of deep learning algorithms, they try to learn high-level features from the data.

This is a very distinctive part of deep learning which makes it way ahead of traditional machine learning deep learning,  reduces the task of developing a new feature extractor for every problem like in the case of the CNN algorithm. It first tries to learn the low-level features of the image such as edges and lines and then it proceeds to the parts of faces of people and then finally to the high-level representation of the face.

Problem-solving approach

The next parameter is the problem-solving approach. When we are solving a problem using a traditional machine learning algorithm. It is generally recommended that we first break down the problem into different sub-parts solve them individually and then finally combine them to get the desired result. This is how the machine learning algorithm handles the problem. On the other hand, the Deep learning algorithm solves the problem from end to end.

Let's take an example


Suppose we have a task of multiple object detection and your task is to identify that what is the object and where it is present in the image.


Deep Learning


 

How we can encounter this issue using the concept of machine learning and deep learning. In a typical machine learning approach, we would first divide the problem into two-step first object detection and then object recognition.

First of all, we can use a bounding box detection algorithm. Once the objects are recognized, an object recognition algorithm, like SVM to recognizes relevant objects. Finally, when you combine the result you would be able to identify that what the object is, and where it is present in the image.

Execution time

Another comparison parameter is the execution time. Usually, a deep learning algorithm takes a long time to train because there are so many parameters in a deep learning algorithm that makes the training longer than usual.

The training might even last for two weeks or more than that. If you are training completely from the scratch. Whereas in the case of machine learning, it relatively takes much less time to train. But, in certain scenarios, the execution time is completely reversed when it comes to the testing of data. During testing, the Deep learning algorithm takes much less time to run. Whereas if you compare it with a KNN algorithm, which is a type of machine learning algorithm the test time increases as the size of the data increase.

Interpretability

Last but not least we have interpretability as a factor for comparison of machine learning and deep learning. This fact is the main reason why deep learning is still thought ten times before anyone new uses it in the industry.

Let's take an example.

Suppose, we use deep learning to give automated scoring to essays. The performance it gives and scoring is quite excellent and is near to the human performance but there's an issue with it. It does not reveal why it has given that score indeed mathematically.

It is possible to find out which node of a deep neural network was activated, but we don't know what the neurons are supposed to model and what these layers of neurons are doing collectively. So, it is difficult to interpret the results.

On the other hand machine learning algorithm, like a decision tree gives us a crisp rule for choosing a node and for not choosing a node. So it is particularly easy to interpret the reasoning behind it. Therefore the algorithms like decision trees, linear or logistic regression are primarily used in industry for interpretability.

Machine learning uses algorithms to parse the data learn from the data and make an informed decision based on what it has learned. In deep learning structures algorithms in layers to create an artificial neural network that can learn and make intelligent decisions on its own. Deep learning is a subfield of machine learning while both fall under the broad category of artificial intelligence. Deep learning is usually what's behind the most human-like artificial intelligence.



Friday, June 4, 2021

String Handling in Java

Strings in Handling in Java

 A String is a standard object in Java of Class String and is provided built-in support. A String is sequence of characters. A String is a final class in Java (it can not be further extended) and the objects of this class are immutable(means these are read-only). The String class’s objects support for +,+=(concatenation) operators. The String class objects are thread-safe. We can use StringBuffer and StringBuilder classes for using a mutable string.

The ability to manipulate alphanumeric data (such as names, addresses, text, etc.) is called String handling. Several methods are provided in String class to handle strings. Few Examples of string handling are listed below along with their solutions.

1. Reverse a given String

Test Case

Input: Hello

Output: olleH

Solution:

import java.util.Scanner;

public class Q1 {
//1.Reverse a given String.
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		System.out.println("Enter some string");
		String s=scan.nextLine();
		StringBuffer sb=new StringBuffer(s);
		String rev=sb.reverse().toString();
		/* Another way to do the same thing
		 * char str[]=s.toCharArray();
		System.out.println("reverse string is:");
		for(int i=s.length()-1;i>=0;i--)
		{
			System.out.print(str[i]);
		}*/
		System.out.println(rev);
	}

}



2. Find the maximum occurring(highest frequency) character in a given String.(ignore case)

Input: Godzilla

Output: L 2

Solution:

import java.util.Scanner;

public class Q2 {
//2.	Find the maximum occurring character in a given String.
	public static void main(String[] args) {
		int arr[]=new int[26];
		Scanner scan=new Scanner(System.in);
		System.out.println("Enter some string");
		String s=scan.nextLine();
		char strarr[]=s.toUpperCase().toCharArray();
		for(char c:strarr)
		{
			if((int)c>=65 && (int)c<90)
			{
			arr[(int)c-65]++;
			}
		}
		int max=0;
		char ch='$';
		for(int j=0;j<arr.length;j++)
		{
			if(arr[j]>max)
			{
				max=arr[j];
				ch=(char)(j+65);
			}
		}
		System.out.println(max+"  "+ch);
		scan.close();
	}

}


3. Remove all duplicates from a given string. (ignore case)

Input : Godzilla

Output: GODZILA

Solution:

import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;

public class Q3 {

	public static void main(String[] args) {
		//3.	Remove all duplicates from a given string.
		//hello
		//helo
		Scanner scan=new Scanner(System.in);
		System.out.println("Enter some string");
		String s=scan.nextLine();
		TreeSet<Character> hs=new TreeSet<>();
		char[] ch=s.toUpperCase().toCharArray();
		for(char c:ch)
		{
			hs.add(c);
		}
		System.out.println("Output");
		for(Character c:hs)
		{
			System.out.print(c);
		}
	}

}


4. Print the unique characters from the given String. (ignore case)

Input : Godzilla

Output: GODZILA

Solution:

import java.util.Scanner;

public class Q4 {
//4.	Print the unique characters from the given String.
	
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		System.out.println("Enter some string");
		String s=scan.nextLine();
		char[] ch=s.toCharArray();
		//{h,e,l,l,o}
		//{h,e,l,$,o}
		for(int i=0;i<ch.length;i++)
		{
			for(int j=i+1;j<ch.length;j++)
			{
				if(ch[i]==ch[j] && ch[i]!='$')
				{
					ch[j]='$';
				}
			}
		}
		System.out.println("unique characters are:");
		for(int i=0;i<ch.length;i++)
		{
			if(ch[i]!='$')
			System.out.print(ch[i]);
		}
	}

}



5. How to print all permutations of a String. (ignore case)

Input: abb

Output: abb abb bab bba bab bba 

Solution:

import java.util.HashSet;
import java.util.Scanner;

public class Q5 {
	// Function to print all the permutations of str
    static void printPermutn(String str, String ans)
    {
  
        // If string is empty
        if (str.length() == 0) {
            System.out.print(ans + " ");
            return;
        }
  
        for (int i = 0; i < str.length(); i++) {
  
            // ith character of str
            char ch = str.charAt(i);
  
            // Rest of the string after excluding 
            // the ith character
            String ros = str.substring(0, i) + 
                         str.substring(i + 1);
  
            // Recurvise call
            printPermutn(ros, ans + ch);
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String s = "abb";
        printPermutn(s, "");
    }
	

}


6. Reverse the words in a given String sentence.

Input: I am not an alien

Output: alien an not am I

Solution:

import java.util.Scanner;

public class Q6 {
	//6.Reverse the words in a given String sentence.
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		System.out.println("Enter some string");
		String s=scan.nextLine();
		String[] strs=s.split(" ");
		for(int i=strs.length-1;i>=0;i--)
		{
			System.out.println(strs[i]);
		}
		scan.close();
	}

}


7. Check if a given String is Palindrome or not.

Input: malayalam

Output: Palindrome

Solution:

import java.util.Scanner;

public class Q7 {
//7. Check if a given String is Palindrome or not.
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		System.out.println("Enter some string");
		String s=scan.nextLine();
		StringBuffer sb=new StringBuffer(s);
		String rev=sb.reverse().toString();
		if(s.equals(rev))
		{
			System.out.println("palindrome");
		}else
		{
			System.out.println("not palindrome");
				
		}
	}

}


8. Check if two Strings are anagrams of each other. (An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once)

Input: Army Mary

Output: Anagram

Solution:

import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeSet;

public class Q8 {
//8. check if two Strings are anagrams of each other
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		System.out.println("Enter first string");
		String s1=scan.nextLine();
		System.out.println("Enter second string");
		String s2=scan.nextLine();
		char[] arr1=s1.toUpperCase().toCharArray();
		char[] arr2=s2.toUpperCase().toCharArray();
		Arrays.sort(arr1);
		Arrays.sort(arr2);
		if(arr1.length!=arr2.length)
		{
			System.out.println("Not anagrams");
			return;
		}
		else
		{
			for(int i=0;i<arr1.length;i++)
			{
			if(arr1[i]!=arr2[i])
			{
				System.out.println("Not anagrams");
				return;
			}
			}
			System.out.println("Anagrams");
		}

		scan.close();

	}

}


9. Sort array(read arrays in Java) of Strings in alphabetic order.

Input: mango apple orange papaya lemon

Output: apple lemon mango orange papaya


Solution:

import java.util.Arrays;

public class Q9 {
//9.Sort array of Strings
	public static void main(String[] args) {
		String[] str= {"mango","apple","orange","papaya","lemon"};
		Arrays.sort(str);
		for(String s:str)
		{
			System.out.print(s);
		}
	}
}