How To Cheque If Ii String Are Anagram Inwards Coffee - Programme Example

Write a Java plan to banking concern gibe if ii String are anagram of each other, is roughly other skilful coding inquiry asked at fresher marking Java Interviews. This inquiry is on like marking of finding pump chemical gene of LinkedList inwards ane pass in addition to swapping ii numbers without using temp variable. By the means ii String are called anagram, if they contains same characters but on dissimilar companionship e.g. army in addition to mary, stop in addition to pots etc. Anagrams are genuinely mix-up of characters inwards String. If y'all are familiar amongst String API, i.e. java.lang.String than y'all tin easily solve this problem. In companionship to banking concern gibe if  Strings are anagram, y'all demand to larn in that place graphic symbol array in addition to come across if they are equal or not. Though y'all tin too role indexOf(), substring() and StringBuffer or StringBuilder  class to solve this question. In this Java program, nosotros volition come across 3 ways to solve this interview questions, in addition to banking concern gibe if ii String are anagram or not. By the way, if y'all are preparing for Java interview, it's skilful to laid some data structures in addition to algorithms questions as well. More often, in that place is ane or to a greater extent than questions from programming, coding in addition to logic inwards these interviews.


Java plan to banking concern gibe if String is anagram

equals method of Arrays, provide true, solely if array contains same length, in addition to each index has same character. 


For simplicity, I conduct maintain left checking if String is aught or empty in addition to converting them into working capital alphabetic character or lowercase, y'all tin create that if y'all want. If Interviewer inquire y'all to write production character code, thus I would advise definitely position those checks in addition to throw IllegalArgumentException for aught String or y'all tin merely provide false. I would personally prefer to provide fake rather than throwing Exception, like to equals() method. Anyway, hither are three ways to banking concern gibe if ii String are Anagram or not. I conduct maintain too included a JUnit Test to verify diverse String which contains both anagram in addition to not.

import java.util.Arrays;  /**  * Java plan - String Anagram Example.  * This plan checks if ii Strings are anagrams or non  *  * @author Javin Paul  */ public class AnagramCheck {         /*      * One means to uncovering if ii Strings are anagram inwards Java. This method      * assumes both arguments are non aught in addition to inwards lowercase.      *      * @return true, if both String are anagram      */     public static boolean isAnagram(String word, String anagram){                if(word.length() != anagram.length()){             return false;         }                 char[] chars = word.toCharArray();                 for(char c : chars){             int index = anagram.indexOf(c);             if(index != -1){                 anagram = anagram.substring(0,index) + anagram.substring(index +1, anagram.length());             }else{                 return false;             }                    }                 return anagram.isEmpty();     }         /*      * Another means to banking concern gibe if ii Strings are anagram or non inwards Java      * This method assumes that both give-and-take in addition to anagram are non aught in addition to lowercase      * @return true, if both Strings are anagram.      */     public static boolean iAnagram(String word, String anagram){         char[] charFromWord = word.toCharArray();         char[] charFromAnagram = anagram.toCharArray();                Arrays.sort(charFromWord);         Arrays.sort(charFromAnagram);                 return Arrays.equals(charFromWord, charFromAnagram);     }             public static boolean checkAnagram(String first, String second){         char[] characters = first.toCharArray();         StringBuilder sbSecond = new StringBuilder(second);                 for(char ch : characters){             int index = sbSecond.indexOf("" + ch);             if(index != -1){                 sbSecond.deleteCharAt(index);             }else{                 return false;             }         }                 return sbSecond.length()==0 ? true : false;     } }

JUnit Test Case for String Anagram Exmaple

hither is our JUnit tests for all iii 3 methods of AnagramCheck class, nosotros conduct maintain genuinely tested all method amongst like laid of input.
import org.junit.Test; import static org.junit.Assert.*;  /**  * JUnit exam course of pedagogy to exam diverse anagram plan for diverse String input.  */ public class StringAnagramTest {           @Test     public void testIsAnagram() {         assertTrue(AnagramCheck.isAnagram("word", "wrdo"));         assertTrue(AnagramCheck.isAnagram("mary", "army"));         assertTrue(AnagramCheck.isAnagram("stop", "tops"));         assertTrue(AnagramCheck.isAnagram("boat", "btoa"));         assertFalse(AnagramCheck.isAnagram("pure", "in"));         assertFalse(AnagramCheck.isAnagram("fill", "fil"));         assertFalse(AnagramCheck.isAnagram("b", "bbb"));         assertFalse(AnagramCheck.isAnagram("ccc", "ccccccc"));         assertTrue(AnagramCheck.isAnagram("a", "a"));         assertFalse(AnagramCheck.isAnagram("sleep", "slep"));             }         @Test     public void testIAnagram() {         assertTrue(AnagramCheck.iAnagram("word", "wrdo"));         assertTrue(AnagramCheck.iAnagram("boat", "btoa"));         assertFalse(AnagramCheck.iAnagram("pure", "in"));         assertFalse(AnagramCheck.iAnagram("fill", "fil"));         assertTrue(AnagramCheck.iAnagram("a", "a"));         assertFalse(AnagramCheck.iAnagram("b", "bbb"));         assertFalse(AnagramCheck.iAnagram("ccc", "ccccccc"));         assertFalse(AnagramCheck.iAnagram("sleep", "slep"));             }          @Test     public void testcheckAnagram() {         assertTrue(AnagramCheck.checkAnagram("word", "wrdo"));                assertFalse(AnagramCheck.checkAnagram("b", "bbb"));         assertFalse(AnagramCheck.checkAnagram("ccc", "ccccccc"));         assertTrue(AnagramCheck.checkAnagram("a", "a"));         assertFalse(AnagramCheck.checkAnagram("sleep", "slep"));         assertTrue(AnagramCheck.checkAnagram("boat", "btoa"));         assertFalse(AnagramCheck.checkAnagram("pure", "in"));         assertFalse(AnagramCheck.checkAnagram("fill", "fil"));             } }  Output Testsuite: StringAnagramTest Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.094 sec

Our AnagramCheck course of pedagogy contains 3 static methods to verify if Strings are anagram or not. First one, takes graphic symbol array of showtime String in addition to loop through it, thus finds that graphic symbol inwards minute String, in addition to deletes it yesteryear using substring method. If minute String doesn't contains graphic symbol than method provide fake immediately. At the destination of exam if minute String is empty than both Strings are anagram because they contains same laid of characters. To meliorate performance, nosotros conduct maintain checked length at really start of this method, equally ii String amongst dissimilar length tin non live on anagram of each other. Third method is precisely same of showtime one, except, it uses deleteCharAt(int index) method of StringBuilder for deleting characters.

Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures in addition to Algorithms: Deep Dive Using Java
Write a Java plan to uncovering nth Fibonacci publish inwards Java

Sumber https://javarevisited.blogspot.com/

0 Response to "How To Cheque If Ii String Are Anagram Inwards Coffee - Programme Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel