What is an anagram in Java?In this section, we will discuss what is anagram in Java and how to check if the given string is an anagram or not. The anagram Java program is frequently asked in Java interviews. AnagramThe dictionary meaning of the word anagram is a word or phrase formed by rearranging the letters. Two strings are said to be anagrams if they make a meaningful word by rearranging or shuffling the letters of the string. In other words, we can say that two strings are anagrams if they contain the same characters but in a different order. Note that a letter has to be used only once. Example of AnagramThere are several anagram words some of them are: LISTEN - > SILENT HEART - > EARTH LIVES - > ELVIS KEEP -> PEEK TABLE - >BLEAT How to check two strings are anagram or not?
Let's implement the above steps in a Java program. Java Anagram ProgramThere is various way to find anagram string but, in this section, we will focus on the following three ways.
Using Arrays ClassIn the following program, we have used to methods String.sort() and equals() method to check if the two strings are anagram or not. AnagramExample1.java Output: HEART and EARTH are anagrams TRIANGLE and INTEGRAL are anagrams TOSS and SHOT are not anagrams Using for LoopAnagramExample2.java Output: true true false Using StringBuilder ClassAnagramExample3.java Output: true false Next TopicWhat is programming |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India