Check Strings are Anagram

An Anagram is a special type of word or phrase where rearranging the letters will create a valid new word.

  • The longest anagram of a single word, which uses all 27 letters is hydroxydeoxycorticosterones and hydroxydesoxycorticosterone
  • Japan's former capital Kyoto and current capital Tokyo are anagrams of each other.
  • The special words whose anagram are Palindromes as well, ie when read in reverse order, it creates a new word are called semordnilap. For example, stressed and desserts.
  • In Harry potter book series, the main antagonist, Lord Voldemort's childhood name was Tom Marvolo Riddle which is an anagram of i am lord voldemort
  • The most expendable synthetic fiber Spandex is an anagram of expands.
  • The special words whose anagrams have Opposite meanings are called Antigrams For example, Santa is the word Satan.
  • The anagrams of eleven plus two is twelve plus one.
package org.wesome.dsalgo;

import java.util.Arrays;

public class Anagram {
    static boolean anagram(String string1, String string2) {
        if (string1.isEmpty() || string2.isEmpty()) {
            throw new RuntimeException("Invalid String");
        }
        if (string1.equalsIgnoreCase(string2)) {
            return true;
        }
        char[] chars1 = string1.toCharArray();
        char[] chars2 = string2.toCharArray();
        Arrays.sort(chars1);
        Arrays.sort(chars2);
        return Arrays.equals(chars1, chars2);
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class AnagramTest {
    @Test
    void anagramTest1() {
        boolean anagram = Anagram.anagram("abcba", "aabbc");
        Assertions.assertTrue(anagram);
    }

    @Test
    void anagramTest2() {
        boolean anagram = Anagram.anagram("wesome.org", "wsm.reoeog");
        Assertions.assertTrue(anagram);
    }

    @Test
    void anagramTest3() {
        boolean anagram = Anagram.anagram("madam", "madam");
        Assertions.assertTrue(anagram);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}

 

package org.wesome.dsalgo;

public class Anagram {
    static boolean anagram(String string1, String string2) {
        if (string1.isEmpty() || string2.isEmpty()) {
            throw new RuntimeException("Invalid String");
        }
        if (string1.equalsIgnoreCase(string2)) {
            return true;
        }
        char[] chars1 = string1.toCharArray();
        char[] chars2 = string2.toCharArray();
        int[] ints = new int[1 << 8];
        for (char c : chars1) {
            ints[c]++;
        }
        for (char c : chars2) {
            ints[c]--;
        }
        for (int i : ints) {
            if (ints[i] != 0) {
                return false;
            }
        }
        return true;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class AnagramTest {
    @Test
    void anagramTest1() {
        boolean anagram = Anagram.anagram("abcba", "aabbc");
        Assertions.assertTrue(anagram);
    }

    @Test
    void anagramTest2() {
        boolean anagram = Anagram.anagram("wesome.org", "wsm.reoeog");
        Assertions.assertTrue(anagram);
    }

    @Test
    void anagramTest3() {
        boolean anagram = Anagram.anagram("madam", "madam");
        Assertions.assertTrue(anagram);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}

 

follow us on