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
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’...
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...
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...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Assume you have two dices: one has 6 faces with letters from A to B (i.e.,...
Assume you have two dices: one has 6 faces with letters from A to B (i.e., A, …, F) and the other has 20 faces with numbers from 1 to 20 (i.e., 1, …, 20)? We assume that the number dice is even, meaning that the number dice gives a number from 1 to 20 with the probability 1/20 of each. However, the letter dice is not even. The letter dice always gives you a letter from A to F...
With some services, e.g., checking accounts, phone service, or pay TV, a consumer is offered a...
With some services, e.g., checking accounts, phone service, or pay TV, a consumer is offered a choice of two or more payment plans. One can either pay a low entry fee and get a high price per unit of service or pay a high entry fee and a low price per unit of service. Suppose you have an income of $1000. There are two plans. Plan A has an entry fee of $20 with a price of $10 per unit....
The value obtained for the test statistic, z, in a one mean z-test is given. Whether...
The value obtained for the test statistic, z, in a one mean z-test is given. Whether the test is two-tailed, left tailed, right tailed is also specified. For parts (a) and (b), determine: a) the test statistic in a two tailed test is z= -0.88. The p-value is : _? At the 1% significance level, the data _____ sufficient evidence to reject the null hypothesis in favor of the alternative hypothesis because the obtained p-value is _______ the significance level....