Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

anagram in java

Mon Sep 05, 2011 6:45 am

Write a program to find if the given two strings are anagram are not. An anagram of a string is a string obtained by permuting the letters of a string. For example aaba and aaab are anagrams, while abcd and deba are not.



Re: anagram in java

Thu Sep 08, 2011 11:49 pm

Code:


import java
.io.IOException;

public class 
ExampleClass {

    static 
int sizeOne;
    static 
int count;
    static 
char[] firstCharArray;
    static 
char[] secondCharArray;

    public static 
void main(String[] argsthrows IOException {
        
String firstString "Java";
        
String secondString "Jvaa";
        
sizeOne firstString.length();
        
count 0;
        
firstCharArray = new char[sizeOne];
        
secondCharArray = new char[sizeOne];
        
// assumption both are equal in size.
        
for (int j 0sizeOnej++) {
            
firstCharArray[j] = firstString.charAt(j);
            
secondCharArray[j] = secondString.charAt(j);
        }
        
startAnagram(sizeOne);

        
System.out.println("The strings are not anagram ");
    }

    
// recursive anagram algorithem
    
public static void startAnagram(int stringSize) {

        if (
stringSize == 1) {
            return;
        }

        for (
int i 0stringSizei++) {
            
startAnagram(stringSize 1);
            if (
stringSize == 2// if innermost,
            
{
                print();
            }
            
rotateAnagram(stringSize); // rotate word
        
}
    }

    
// rotate left all chars from position to end
    
public static void rotateAnagram(int currentSize) {
        
int i;
        
int position sizeOne currentSize;
        
// temp for current letter.
        
char temp firstCharArray[position];
        
//shift others left
        
for (position 1sizeOnei++) {
            
firstCharArray[1] = firstCharArray[i];
        }
        
//put first on right
        
firstCharArray[1] = temp;
    }

    public static 
void print() {

        for (
int i 0sizeOnei++) {
            
System.out.print(firstCharArray[i]);
        }
        
System.out.println("");
        
checkIfStringsEqual();
    }

    public static 
void checkIfStringsEqual() {

        
boolean flag true;

        for (
int i 0sizeOnei++) {
            if (
firstCharArray[i] != secondCharArray[i]) {
                
flag false;
                break;
            }
        }
        if (
flag) {
            
System.out.println("The strings are anagram ");
            
System.exit(1);
        }

    }
}

 


Re: anagram in java

Fri Sep 16, 2011 1:35 pm

Short version:
Code:
public static void main(String[] arg) {
      String str1 = "hello";
      String str2 = "olleh";

      if (isAnagram(str1, str2)) {
        System.out.println("Anagram");
      } else {
        System.out.println("Not Anagram");
      }
  }

  private static boolean isAnagram(String str1, String str2) {
    if (str1.length() != str2.length()) return false;
    for (int i = 0; j = str2.length() -1; i <= j; i++, j--) {
      if (str1.charAt(i) != str2.charAt(j)) {
        return false;
      }
    }
    return true;
  }


Post a reply
  Related Posts  to : anagram in java
 Java Anagram     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 Java course     -  
 need help in java     -  
 What is Java API?!!!     -  
 java or .net     -  
 Using FTP in java     -  
 what is java     -  
 Java - Multithreading     -  
 features of java     -  

Topic Tags

Java Algorithms, Java Strings