Home > Enterprise >  How to sort a String and check for anagrams?
How to sort a String and check for anagrams?

Time:11-21

I'm supposed to write an algorithm that tests two Strings to check if they're anagrams of each other and I have to use either BubbleSort, SelectionSort or InsertionSort. So I used SelectionSort to sort the Strings, which I converted to char arrays beforehand, but it doesn't work and I cannot find my mistake.

public static void selectionSort(char[] arr) {

    for (int i = 0; i < arr.length - 1; i  ) {
        int least = i;
        for (int j = i   1; j < arr.length; j  ) {
            if (arr[j] < arr[least])
                least = j;
            if (least != i) {
                int swap = arr[i];
                arr[i] = arr[least];
                arr[least] = (char) swap;

            }
        }
    }
}

public static boolean anagramCheck(String x, String y) {

    x.trim();
    y.trim();

    x.toLowerCase();
    y.toLowerCase();

    char xarr[] = x.toCharArray();
    char yarr[] = y.toCharArray();

    if (x.length() != y.length())
        return false;

    selectionSort(xarr);
    System.out.println(xarr); // I used this to check if the Strings are sorted correctly
    selectionSort(yarr);
    System.out.println(yarr);

    if (xarr == yarr) {
        System.out.println("It's an anagram.");
        return true;
    } else {
        return false;
    }
}

I'm supposed to ignore capital letters and spaces, that's why I used trim() and toLowerCase(). But it neither trims the spaces nor changes capital letters to lower case letters. Additionally, when I use more than 5 letters, it doesn't sort the given Strings alphabetically. Only one of both is sorted correctly, the other one is messed up. I am new to Java programming so I might need some help here. Thanks in advance

CodePudding user response:

This should work. But I used bubbleSort

class HelloWorld {
  public static void main(String[] args) {
      System.out.println(anagramCheck("listen", "silent"));
  }
  public static boolean anagramCheck(String x, String y) {

    x.trim();
    y.trim();

    x.toLowerCase();
    y.toLowerCase();

    char xarr[] = x.toCharArray();
    char yarr[] = y.toCharArray();

    if (x.length() != y.length())
       return false;

    x = bubbleSort(xarr);
    System.out.println(xarr); // I used this to check if the Strings are sorted correctly

    y = bubbleSort(yarr);
    System.out.println(yarr);

    if (x.equals(y)) {
       System.out.println("It's an anagram.");
       return true;
    } else {
        return false;
    }
   }

   public static String bubbleSort(char[] arr) {

    int n = arr.length;
    for (int i = 0; i < n-1; i  ){
        for (int j = 0; j < n-i-1; j  ){
            if (arr[j] > arr[j 1])
            {
                // swap arr[j 1] and arr[j]
                char temp = arr[j];
                arr[j] = arr[j 1];
                arr[j 1] = temp;
            }
        }
    }
    
    return String.valueOf(arr);
  }

}

CodePudding user response:

Check where i did modification.

public static void selectionSort(char[] arr) {

for (int i = 0; i < arr.length - 1; i  ) {
    int least = i;
    for (int j = i   1; j < arr.length; j  ) {
        if (arr[j] < arr[least])
            least = j;
    }
    if (least != i) {
         int swap = arr[i];
         arr[i] = arr[least];
         arr[least] = (char) swap;

    }
  }
}

In anagramCheck function:

x = x.trim(); // it returns trim string

y = y.trim();

x = x.toLowerCase(); // return lower-case string

y = y.toLowerCase();

CodePudding user response:

Bro string is immutable and all methods on String produces a new instance. You have to assign the result of the value to the variable, like newx=x.trim().toLowerCase();

I cannot fix any bug in the algorithm, I am actually very bad at algorithms

  • Related