Question

Anagram checking Design an algorithm for checking whether two given words are anagrams, i.e., whether one...

Anagram checking Design an algorithm for checking whether two given words are anagrams, i.e., whether one word can be obtained by permuting the letters of the other. (For example, the words tea and eat are anagrams.)

Homework Answers

Answer #1

import java.util.Scanner;

public class HelloWorld{

public static void main(String []args){
Scanner scanner = new Scanner(System.in);
String str1, str2;
int[] str1LetterCount = new int[26];
int[] str2LetterCount = new int[26];
boolean isAnagram = true;
  
System.out.print("Enter string 1: ");
str1 = scanner.nextLine();
System.out.print("Enter string 2: ");
str2 = scanner.nextLine();

//Maintain count of all the letters present in first string
for(int i=0; i < str1.length(); i++) {
str1LetterCount[str1.toLowerCase().charAt(i) - 'a']++;
}
  
//Maintain count of all the letters present in second string
for(int i=0; i < str2.length(); i++) {
str2LetterCount[str2.toLowerCase().charAt(i) - 'a']++;
}

// Check whether each letter count is same in both the strings. If they
// are equal then the string are anagram otherwise not.
for(int i=0; i < 26; i++) {
if(str1LetterCount[i] != str2LetterCount[i]) {
isAnagram = false;
break;
}
}
if(isAnagram){
System.out.println("Strings are anagram");
}
else {
System.out.println("Strings are not anagram");
}
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Two words form a "metathesis pair" if you can transform one into the other by swapping...
Two words form a "metathesis pair" if you can transform one into the other by swapping two letters; for example, "converse" and "conserve". Write a function metathesis(word1, word2) to check if the two words can form a metathesis pair. The function will return a Boolean value (True or False). python code
Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is...
Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is given a positive integer N and determines whether N is a prime number, that is, not evenly divisible by any value other than 1 and itself. The output of your algorithm is either the message ‘not prime’, along with a factor of N, or the message ‘prime ‘ Many excellent simulations of sorting algorithms are available, examine them if they have questions about this...
design two tests. One to determine whether or not a defendant is fit to stand trial...
design two tests. One to determine whether or not a defendant is fit to stand trial and the other to determine if the offender is not criminally responsible on account of mental disorder (NCRMD). Describe the components you would test in each case. Identify how these two assessments are similar to each other. Identify how these two assessments are distinct from each other
Write a Turing-machine style of algorithm to decide the language L1 given below. Use specific, precise,...
Write a Turing-machine style of algorithm to decide the language L1 given below. Use specific, precise, step-by-step English. So, describe how to test whether or not an input string is in the language L1 in finite time. No need to write a state diagram. L1 = {w : every ‘a’ within w is to the left of every ‘b’ within w} over the following alphabet Σ = {a, b, c}. In other words, you’re not allowed to have any ‘b’...
In Java In Scrabble each player has a set of tiles with letters on them. The...
In Java In Scrabble each player has a set of tiles with letters on them. The object of the game is to use those letters to spell words. The scoring system is complex, but longer words are usually worth more than shorter words. Imagine you are given your set of tiles as a string, like "quijibo", and you are given another string to test, like "jib". Write a method called canSpell that takes two strings and checks whether the set...
A perceptual psychologist speculated that people can identify letters faster if they are surrounded by other...
A perceptual psychologist speculated that people can identify letters faster if they are surrounded by other letters in real words compared to letters in meaningless combinations.   For example, people should recognize the letter c in the word doctor faster than c in the letter stringtocdor.   He created a letter identification task where the stimuli were identical except in one condition the target letter was embedded in a word and in a second condition the letter was embedded in scrambled letters.   He created 200...
Given a set of n distinct bolts and n corresponding nuts, (a one-to-one correspondence exists between...
Given a set of n distinct bolts and n corresponding nuts, (a one-to-one correspondence exists between bolts and nuts), we want to find the correspondence between them. We are not allowed to directly compare two bolts or two nuts, but we can compare a bolt with a nut to see which one is bigger. Design an algorithm to find the matching pairs of bolts and nuts in time O(n2) for the worst-case scenario. Your algorithm should have an expected running...
A perceptual psychologist speculated that people can identify letters faster if they are surrounded by other...
A perceptual psychologist speculated that people can identify letters faster if they are surrounded by other letters in real words compared to letters in meaningless combinations.   For example, people should recognize the letter c in the word doctor faster than c in the letter stringtocdor.   He created a letter identification task where the stimuli were identical except in one condition the target letter was embedded in a word and in a second condition the letter was embedded in scrambled letters.   He created...
Can you plzz do part D . Transform and Conquer Design a reasonably efficient algorithm for...
Can you plzz do part D . Transform and Conquer Design a reasonably efficient algorithm for solving each of the following problems and determine its efficiency class. a. You are given n telephone bills and m checks sent to pay the bills (n ≥ m). Assuming that telephone numbers are written on the checks, find out who failed to pay. (For simplicity, you may also assume that only one check is written for a particular bill and that it covers...
a. Design a FSA that will recognize sentences such as “The dog that chased the cat...
a. Design a FSA that will recognize sentences such as “The dog that chased the cat that ate the mouse lived in the house that Jack built.” Namely, the sentences should look like “The ANIMAL1 that VERBED1 the ANIMAL2 that VERBED2 the ANIMAL3 … VERBED3 in the house that Jack built.”, where ANIMALn is an animal and VERBEDn is a past-tense verb, and where the maximum n can be arbitrarily large. Assume that words are fed to the FSA one...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Your company is thinking of introducing a Bring Your Own Device (BYOD) policy. You have been...
    asked 3 minutes ago
  • Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
    asked 5 minutes ago
  • Suppose the number of cars in a household has a binomial distribution with parameters n =...
    asked 8 minutes ago
  • HR needs some information on the new interns put into a database. Given an id, email,...
    asked 29 minutes ago
  • Problem solving strategies Questions years = input("Enter a number of years and I'll tell you how...
    asked 33 minutes ago
  • Calculate ?Hrxn for the following reaction: CH4(g)+4Cl2(g)?CCl4(g)+4HCl(g) Use the following reactions and given ?H?s. C(s)+2H2(g)?CH4(g)?H=?74.6kJC(s)+2Cl2(g)?CCl4(g)?H=?95.7kJH2(g)+Cl2(g)?2HCl(g)?H=?184.6kJ Express...
    asked 40 minutes ago
  • ASCII (American Standard Code for Information Interchange) has an encoding for every character of the alphabet,...
    asked 54 minutes ago
  • Is home confinement with electronic monitoring a deterrent? Are there negatives to being confined to one’s...
    asked 1 hour ago
  • Social hostility can have severe lasting effects of interperpersonal relationship during our adolescence years, which if...
    asked 1 hour ago
  • - A series RLC circuit has R=15 ?, L=1.5 H, and C=15 ?F. (a) For what...
    asked 1 hour ago
  • TV Circuit has 30 large-screen televisions in a warehouse in Erie and 60 large-screen televisions in...
    asked 1 hour ago
  • Charges q1, q2, q3, and q4 are placed in sequential order at the corners of a...
    asked 1 hour ago