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
  • Consider the encryption code  Encryption Code true false - stringContains is an encryption algorithm that can be...

    Consider the encryption code  Encryption Code true false - stringContains is an encryption algorithm that can be decrpyted true false - Running reverseString twice will have no effect true false - Running incLetters(s,4) then incLetters(s,-4) will have no effect true false - using maxDigit as an encryption algorighm, it can be decrypted USE CODE BELOW: package encryption; import java.util.Scanner; public class Encryption { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); String password, encryptedPassword, salt; int increment;...

  • Write a program that stores a phrase as an array of words, and then prints it...

    Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{    public static void main(String[] args) {    String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...

  • 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();   ...

  • I am trying to run a couple of functions in my main and It won't output...

    I am trying to run a couple of functions in my main and It won't output anything if there is anything that you see wrong please help me out. I have to use simple, sometimes slower ways because we can use what we haven't learned yet. Here is my code. package a3; import java.util.Scanner; public class LoopsAndImages { public static void main(String[] args){ String test = "A rabbit has a carrot"; Boolean numbers = hasMoreEvenThanOdd("12344"); System.out.println(test); System.out.println(hideLetterA(test)+" This is the...

  • 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:...

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