Question

Background An anagram is a word phrase, or name formed by rearranging the letters of another, such as the word cinema, formExamples Example 1: Input: bear bare Output: true Example 2: Input: rAce Care Output: trueExample 3: Input: red raw drawer Output: true Example 4: Input: Queens College Computer Science Output: falseSolution.java Load default template... import java.util.Scanner; public class Solution { public boolean isAnagram(String s, S

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code You provided is working fine .I explained the code in comments part .

Source code:

import java.util.*;
public class Solution
{
    public boolean isAnagram(String s,String t)
    {
        s=s.toLowerCase(); //converting string into toLowerCase
        t=t.toLowerCase(); //converting string into to lowercase
        int count[]=new int[26]; //count occurance of each Character
        //intially count array is 0 ===>[0,0,0,0,0.......0(26 0's)]
        for(int i=0;i<s.length();i++) //loop-1
        {
            //if any Character in between a-z occurs correspondig index is incremented by one
            //example present character is 'a' count[s.charAt[i]-'a']++=>count[0]++
            //similary present character is 'z' count[s.charAt[i]-'a']++=>count[25]++
            if(s.charAt(i)>='a' && s.charAt(i)<='z')
                count[s.charAt(i)-'a']++;
        }
        for(int i=0;i<t.length();i++) //loop-2
        {
            //if any Character in between a-z occurs correspondig index is decremented by one
            //example present character is 'b' count[s.charAt[i]-'a']++=>count[1]--
            //similary present character is 'y' count[s.charAt[i]-'a']++=>count[24]--
            if(t.charAt(i)>='a' && t.charAt(i)<='z')
                count[t.charAt(i)-'a']--;
        }
        //for Anagram words after performing loop1 ,loop2 count arrays becomes zero [0,0,0,0,0,.....(26 0's)]
        //below loop used to check Anagram conditon
        for(int i=0;i<26;i++)
        {
            //we check whether any extra characters present or not
            //count array must be 0 otherwise either String s or String t contains extra characters so it is not Anagram word
            if(count[i]!=0)
            {
                return false;
            }
        }
        return true;
    }
   public static void main(String[] args) {
       Scanner scnr=new Scanner(System.in);
        Solution test=new Solution();
       String a=scnr.nextLine(); //reading string a
       String b=scnr.nextLine(); //reaing string b
       boolean out=(test.isAnagram(a,b))?true:false; //calling isAnagram function
       System.out.println((out));
   }
}

Code screenshot:

1 import java.util.*; 2 public class Solution 3- { public boolean isAnagram(String s,String t) S=s.toLowerCase(); //convertin

public static void main(String[] args) { Scanner scnr=new Scanner(System.in); Solution test=new solutions string_a=scr.nextLi

Output

run-1:
race Care true -

run-2:

assa ssaas false

Add a comment
Know the answer?
Add Answer to:
Background An anagram is a word phrase, or name formed by rearranging the letters of another,...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • Two words or phrases in English are anagrams if their letters (and only their letters), rearranged,...

    Two words or phrases in English are anagrams if their letters (and only their letters), rearranged, are the same. We assume that upper and lower case are indistinguishable, and punctuation and spaces don't count. Two phrases are anagrams if they contain exactly the same number of exactly the same letters, e.g., 3 A's, 0 B's, 2 C's, and so forth. Some examples and non-examples of regular anagrams: * The eyes / they see (yes) * moo / mo (no) *...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • Implement the function hasDuplicates. Input will be given as a line containing a positive integer n,...

    Implement the function hasDuplicates. Input will be given as a line containing a positive integer n, followed by n rows, each containing a string. The output should be either the word true if there are any duplicates among these strings or false if there are not. Your code should work well on long lists of strings. Use the following set-up to write the code in JAVA import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) {...

  • Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly...

    Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly typed and statically typed Everything has a data type & data types must be declared Case sensitive Object oriented System.out.print() vs. System.out.println() How to use the Scanner class to obtain user input Data typesWhat are they? Know the basic types like: int, double, boolean, String, etc. Variables What is a variable? Declarations Initialization Assignment Constants Reserved words like: What is a reserved word? Examples:...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make...

    Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make a word. This type of arrangement is generally referred to as an anagram, it's known as a permutation in mathematics. This assignment will give you some experience thinking about and writing recursive functions. Write a C++ program that searches for ``anagrams'' in a dictionary. An anagram is a word obtained by scrambling the letters of some string. For example, the...

  • JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of...

    JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of a repeated value with one occurrence of the value and a count of how many times to repeat it. This works reasonably well when there are lots of long repeats such as in black and white images. To avoid having to represent non-repeated runs with a count of 1 and the value, a special value is often used to indicate a run and everything...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT