We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Strings
- Java Anagrams
- Discussions
Java Anagrams
Java Anagrams
+ 0 comments Simple Easy Java 7 Solution
**If you find it helpful the please upvote it **
static boolean isAnagram(String s1, String s2) { // Complete the function String a = s1.toLowerCase(); String b = s2.toLowerCase(); char []a1 = a.toCharArray(); char []b1 = b.toCharArray(); java.util.Arrays.sort(a1); java.util.Arrays.sort(b1); if(a1.length!= b1.length) return false; int n = a1.length; // we can write int n = b1.length as well for(int i = 0;i < n; i++){ if(a1[i] != b1[i]) return false; } return true; }
+ 0 comments static boolean isAnagram(String a, String b) { String strA = a.toLowerCase(); String strB = b.toLowerCase();
char[] charA = strA.toCharArray(); char[] charB = strB.toCharArray(); java.util.Arrays.sort(charA); java.util.Arrays.sort(charB); boolean result = java.util.Arrays.equals(charA, charB); return result; }
+ 1 comment public static boolean isAnagram(String a, String b) { a = a.toLowerCase(); b = b.toLowerCase(); char[] ac = a.toCharArray(); char[] bc = b.toCharArray(); java.util.Arrays.sort(ac); java.util.Arrays.sort(bc); return String.valueOf(ac).equals(String.valueOf(bc)); }
+ 0 comments static boolean isAnagram(String a, String b) { if(a.length() != b.length()) { return false; } char[] a1 = a.toLowerCase().toCharArray(); char[] b1 = b.toLowerCase().toCharArray(); java.util.Arrays.sort(a1); java.util.Arrays.sort(b1); return java.util.Arrays.equals(a1, b1); }
+ 0 comments I think it's the one of the most efficient way to solve this problem)
static boolean isAnagram(String a, String b) { a = a.toUpperCase(); b = b.toUpperCase(); if(a.length() != b.length()) { return false; } int[] array = new int[256]; for(int i = 0; i < a.length();i++) { array[a.charAt(i)]++; array[b.charAt(i)]--; } for(int i : array){ if(i != 0){ return false; } } return true; }
Load more conversations
Sort 2001 Discussions, By:
Please Login in order to post a comment