Anagram algorithm in java

An easier way might be to just sort the chars in both strings, and compare whether they are equal: public static boolean isAnagram(String s1, String s2){ // Early termination check, if strings are of unequal lengths, // then they cannot be anagrams if ( s1.length() != s2.length() ) { return false; } s1=s1.toLowerCase(); s2=s2.toLowerCase(); char[] … Read more

How to refacor a code use only loops and simple arrays?

public class Anagram { public static void main(String[] args) { String text = “!Hello123 “; char[] chars = text.toCharArray(); int left = 0; int right = text.length() – 1; while (left < right) { boolean isLeftLetter = Character.isLetter(chars[left]); boolean isRightLetter = Character.isLetter(chars[right]); if (isLeftLetter && isRightLetter) { swap(chars, left, right); left++; right–; } else { … Read more

All possible Angram String combinations in java [closed]

Check this one: public class Test { public static void main(String[] args) { String input = “Wolf”; permutation(input, “”); } private static void permutation(String input, String sofar) { if (input.equals(“”)) { System.out.printf(“%s,”, sofar); } for (int i = 0; i<input.length(); i++) { char c = input.charAt(i); if (input.indexOf(c, i + 1) != -1) continue; permutation(input.substring(0, … Read more