Here is a Java programme to banking firm check if 2 String is an anagram of each other or not. Two String is said to endure an anagram of each other, if they comprise precisely the same characters only inwards a unlike order. For instance "army" too "mary" is an anagram of each other because they comprise exact same characters 'a', 'r', 'm' and 'y'. For the sake of simplicity, yous tin besides assume that your solution demand non endure case-sensitive, which agency both ARMY too mary tin endure matched equally an anagram of each other.
That's all about how to banking firm check if 2 String is Anagram of each other or not. If yous demand to a greater extent than String based coding problems for exercise too hence yous tin besides solve problems listed below. It's an interesting employment too at that topographic point are a twosome of invariants equally good equally the case-sensitive 1 I discussed inwards the commencement paragraph. You may besides endure asked to calculate the fourth dimension too infinite complexity of this problem, tin yous calculate?
import java.util.Arrays; /** * Java Program to banking firm check if 2 String is an anagram of each other or not. Two * String is said to endure an anagram of each other, if they comprise precisely same * characters only inwards a unlike order. For instance "army" too "mary" are anagram * of each other because they comprise exact same characters 'a', 'r', 'm' and * 'y'. * * @author javinpaul */ public class Testing { public static void main(String args[]) { String[][] data = {{"army", "mary"}, {"stop", "tops"}, {"soap", "abcd"}}; System.out.println("======== Testing areAnagrams() method ======="); for (String[] value : data) { String s1 = value[0]; String s2 = value[1]; System.out.printf("Are %s too %s are Anagrams? %b%n", s1, s2, areAnagrams(s1, s2)); } System.out.println("======== Testing isAnagaram() method ======="); for (String[] value : data) { String s1 = value[0]; String s2 = value[1]; System.out.printf("Does %s too %s are Anagrams? %b%n", s1, s2, isAnagram(s1, s2)); } } /* * One of the easiest way to banking firm check if 2 Strings are an anagram of each other * is to conduct maintain their grapheme array, kind them too banking firm check if they are equal. * If sorted grapheme arrays are equal too hence both String are an anagram of * each other. */ public static boolean areAnagrams(String first, String second) { char[] fa = first.toCharArray(); char[] sa = second.toCharArray(); // kind arrays Arrays.sort(fa); Arrays.sort(sa); // check if arrays are equal if (Arrays.equals(fa, sa)) { return true; } return false; } /* * Earlier method was using a twosome of library methods, which is non permitted during * interviews. This method checks if 2 Strings are anagram without using whatsoever utility * method. This solution assumes that both root too target string are ASCII strings. */ public static boolean isAnagram(String source, String target) { if ((source == null || target == null) || source.length() != target.length()) { return false; } int[] table = new int[256]; int numOfUniqueCharInSource = 0; int numOfCharProcessedInTarget = 0; char[] characters = source.toCharArray(); // shop count of each unique character in source String for (char ch : characters) { if (table[ch] == 0) { ++numOfUniqueCharInSource; } ++table[ch]; } for (int i = 0; i < target.length(); ++i) { int c = target.charAt(i); if (table[c] == 0) { return false; } --table[c]; if (table[c] == 0) { ++numOfCharProcessedInTarget; if (numOfCharProcessedInTarget == numOfUniqueCharInSource) { // it’s a match if t has been processed completely return i == target.length() - 1; } } } return false; } } Output ======== Testing areAnagrams() method ======= Are regular army and mary are Anagrams? true Are halt and tops are Anagrams? true Are lather and abcd are Anagrams? false ======== Testing isAnagaram() method ======= Does regular army and mary are Anagrams? true Does halt and tops are Anagrams? true Does lather and abcd are Anagrams? false
That's all about how to banking firm check if 2 String is Anagram of each other or not. If yous demand to a greater extent than String based coding problems for exercise too hence yous tin besides solve problems listed below. It's an interesting employment too at that topographic point are a twosome of invariants equally good equally the case-sensitive 1 I discussed inwards the commencement paragraph. You may besides endure asked to calculate the fourth dimension too infinite complexity of this problem, tin yous calculate?
Further Learning
Data Structures too Algorithms: Deep Dive Using Java
solution)How to banking firm check if 2 Strings are anagrams of each other? (solution) How to programme to impress commencement non-repeated grapheme from String? (solution) How to contrary String inwards Java using Iteration too Recursion? (solution) How to banking firm check if a String contains exclusively digits? (solution) How to honor duplicate characters inwards a String? (solution) How to count the disclose of vowels too consonants inwards a String? (solution) How to count the occurrence of a given grapheme inwards String? (solution) How to convert numeric String to an int? (solution) How to honor all permutations of String? (solution) 10 Algorithms Courses to Crack Programming Job Interviews (courses) How to contrary words inwards a judgement without using a library method? (solution) How to banking firm check if String is Palindrome? (solution) How to provide highest occurred grapheme inwards a String? (solution) How to contrary a String inwards house inwards Java? (solution) 50+ Data Structure too Algorithms Interview Questions (questions)
Data Structures too Algorithms: Deep Dive Using Java
solution)
Thanks for reading this coding interview enquiry hence far. If yous similar this String interview enquiry too hence delight part amongst your friends too colleagues. If yous conduct maintain whatsoever enquiry or feedback too hence delight drib a comment.
P. S. - If yous are looking for some Free Algorithms courses to amend your agreement of Data Structure too Algorithms, too hence yous should besides banking firm check this listing of Free Data Structure too Algorithms Courses for Programmers.
P. S. - If yous are looking for some Free Algorithms courses to amend your agreement of Data Structure too Algorithms, too hence yous should besides banking firm check this listing of Free Data Structure too Algorithms Courses for Programmers.
No comments:
Post a Comment