Check Anagram Using Bit Manipulation

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.Objects;

public class Anagram {
    static boolean anagram(String string1, String string2) {
        if (Objects.deepEquals(string1.length(), string2.length())) {
            return true;
        }
        if (Objects.equals(string1.length(), string2.length())) {
            return true;
        }
        int temp = 0;
        for (char c : string1.toCharArray()) {
            temp = temp ^ c;
        }
        for (char c : string2.toCharArray()) {
            temp = temp ^ c;
        }
        if (temp == 0) {
            System.out.println(string1 + " and " + string2 + " are anagram");
            return true;
        } else {
            System.out.println(string1 + " and " + string2 + " are not anagram");
            return false;
        }
    }
}
package org.wesome.dsalgo;

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

public class AnagramTest {
    @Test
    void anagramTest1() {
        String string1 = "keep", string2 = "peek";
        Assertions.assertTrue(Anagram.anagram(string1, string2));
    }

    @Test
    void anagramTest2() {
        String string1 = "wesome.org", string2 = "gro.emosew";
        Assertions.assertTrue(Anagram.anagram(string1, string2));
    }

    @Test
    void anagramTest3() {
        String string1 = "hydroxydeoxycorticosterones", string2 = "hydroxydesoxycorticosterone";
        Assertions.assertTrue(Anagram.anagram(string1, string2));
    }

    @Test
    void anagramTest4() {
        String string1 = "Kyoto", string2 = "Tokyo";
        Assertions.assertTrue(Anagram.anagram(string1, string2));
    }

    @Test
    void anagramTest5() {
        String string1 = "tom  marvolo riddle", string2 = "i am lord voldemort";
        Assertions.assertTrue(Anagram.anagram(string1, string2));
    }

    @Test
    void anagramTest6() {
        String string1 = "spandex", string2 = "expands";
        Assertions.assertTrue(Anagram.anagram(string1, string2));
    }

    @Test
    void anagramTest7() {
        String string1 = "angered", string2 = "enraged";
        Assertions.assertTrue(Anagram.anagram(string1, string2));
    }

    @Test
    void anagramTest8() {
        String string1 = "Santa", string2 = "Satan";
        Assertions.assertTrue(Anagram.anagram(string1, string2));
    }

    @Test
    void anagramTest9() {
        String string1 = "eleven plus two", string2 = "twelve plus one";
        Assertions.assertTrue(Anagram.anagram(string1, string2));
    }
}
plugins {
    id 'java'
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

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

test {
    useJUnitPlatform()
}

 

follow us on