Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other.
We strongly recommend that you click here and practice it, before moving on to the solution.
Program: public class Anagram { public static void main(String[] args) { String input1 = "d1asu"; String input2 = "adsu"; System.out.println("Given string are input1: " + input1 + ", input2: " + input2); System.out.println("" + isAnagram(input1, input2)); } private static String isAnagram(String input1, String input2) { String result = "Given strings are not anagram"; boolean isAnagram = false; if (!input1.isEmpty() && !input2.isEmpty() && (input1.length() == input2.length())) { char[] arr = input1.toCharArray(); StringBuilder sb = new StringBuilder(input2); for (char a : arr) { int index = sb.indexOf("" + a); if (index != -1) { sb.deleteCharAt(sb.indexOf("" + a)); } } isAnagram = sb.toString().isEmpty(); } if (!isAnagram) { return result; } return "Given strings is anagram"; } } Output: Given string are input1: d1asu, input2: adsu Given strings are not anagram Reference: https://www.devglan.com/java-programs/anagram-test https://www.geeksforgeeks.org/check-whether-two-strings-are-anagram-of-each-other/
Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet.
Examples : The quick brown fox jumps over the lazy dog ” is a Pangram [Contains all the characters from ‘a’ to ‘z’]
“The quick brown fox jumps over the dog” is not a Pangram [Doesn’t contains all the characters from ‘a’ to ‘z’, as ‘l’, ‘z’, ‘y’ are missing]
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
We create a mark[] array of Boolean type. We iterate through all the characters of our string and whenever we see a character we mark it. Lowercase and Uppercase are considered the same. So ‘A’ and ‘a’ are marked in index 0 and similarly ‘Z’ and ‘z’ are marked in index 25.
After iterating through all the characters we check whether all the characters are marked or not. If not then return false as this is not a pangram else return true.
Program:
public class Panagram {
public static void main(String[] args) { String input = "The quick brown fox jumps over the lazy dog"; System.out.println("Panagram test string : "+input); System.out.println(""+ checkPanagram(input)); } /** *Returns true if the string is pangram else false * @param input * @return */ private static String checkPanagram(String input) { String result = "Given string is not panagram"; // Create a hash table to mark the
// characters present in the string
// By default all the elements of
// mark would be false. boolean[] mark = new boolean[26]; if(input == null) { return "Given input is invalid"; } // For indexing in mark[] int index = 0; // Traverse all characters for(int i=0;i // If uppercase character, subtract 'A'
// to find index. if( 'A' <= input.charAt(i) && input.charAt(i) <= 'Z') { index = input.charAt(i) - 'A'; System.out.println("current value: "+input.charAt(i)+", index :"+index); } // If lowercase character, subtract 'a'
// to find index. if( 'a' <= input.charAt(i) && input.charAt(i) <= 'z') { index = input.charAt(i) - 'a'; System.out.println("current value: "+input.charAt(i)+", index :"+index); } // Mark current character mark[index] = true; } System.out.println("mark: "+mark+", length: "+mark.length); // Return false if any character is unmarked for(int j =0;jif(mark[j] == false) { return result; } } // If all characters were present return "The Given String is panagram."; }
}
Output:
Panagram test string : The quick brown fox jumps over the lazy dog
current value: T, index :19
current value: h, index :7
current value: e, index :4
current value: q, index :16
current value: u, index :20
current value: i, index :8
current value: c, index :2
current value: k, index :10
current value: b, index :1
current value: r, index :17
current value: o, index :14
current value: w, index :22
current value: n, index :13
current value: f, index :5
current value: o, index :14
current value: x, index :23
current value: j, index :9
current value: u, index :20
current value: m, index :12
current value: p, index :15
current value: s, index :18
current value: o, index :14
current value: v, index :21
current value: e, index :4
current value: r, index :17
current value: t, index :19
current value: h, index :7
current value: e, index :4
current value: l, index :11
current value: a, index :0
current value: z, index :25
current value: y, index :24
current value: d, index :3
current value: o, index :14
current value: g, index :6
mark: [Z@70dea4e, length: 26
The Given String is panagram.
In product based companies are looking for coding, algorithms, design in depth knowledge.
So we need to concentrate below Cracking The Coding in the first step, for the Gayle giving good tips with examples. I have provided all the web links for the getting good knowledge coding and algorithms. Up coming post will add good design prospective links.
Build Your Own Test Framework
-
[image: Build Your Own Test Framework]
Learn to write better automated tests that will dramatically increase your
productivity and have fun while doing so...
EndOfLife Software packages
-
The "End of Life" (EOL) page for Software provides information on the
release dates, support periods, and security status of different Software
versions. I...
How to Install Java on Ubuntu 24.04
-
Connect to us ( @twitter | @facebook )
Java is a versatile programming language that can be installed on Ubuntu
24.04 using different methods, such as “a...
Demystifying the Top 10 Spring Annotations for 2024
-
Introduction:
Spring annotations are a powerful tool for autoconfiguring Spring and
streamlining the wiring up of components. They serve as metadata that
...
Vedic Maths Classes Online - Sydney
-
If you are keen to learn Vedic Maths, and have full commitment and passion
to do so, please reach out to me at this email address for online classes
- stud...
Mini-Review Roundup
-
*Mini-Review Roundup*
[image: Marie Antoinette: The Journey]
*Marie Antoinette: The Journey by Antonia Fraser*
Honestly, this was kind of a letdown...
Java URL Encoder/Decoder Example
-
Java URL Encoder/Decoder Example – In this tutorial we will see how to URL
encode/decode attributes in Java. Overview URL encoding, is a mechanism
for enco...
Top Developer Collaboration Tools
-
How to drive your project into a corner? Just in case you wondered, there
are multiple options. The surest one is miscommunication. Considering that
you ca...
Meet The Wattpad Stars: Brittany Geragotelis
-
After 10 years of pounding the pavement with publishers and agents,
Brittany Geragotelis decided to break free from gatekeepers by posting an
original stor...
My Notes On Time Series
-
Time Series
Data set for which time lies in one of the axis is known as time series.
Most of the economical data, meteorological data is an example of time...
OSGi - Road Ahead
-
OSGi has been introduced many times in the past few years and will still
require some in the future1. With so many introductions, one might be
inclined to ...
The Two Ways of Doing a Job
-
Whether it's deployment, development, performance tuning, troubleshooting
or something else, there are two fundamentally different ways of doing your
job: ...
Blog Members
-
Here is how to show off your blog members. These are members of your blog
community who have joined your blog through Google Friend Connect. Put all
your m...