Question

WRITTEN IN JAVA Write a function as follows: Receive a string as a parameter.Count the length...

WRITTEN IN JAVA Write a function as follows:

Receive a string as a parameter.Count the length of the string.Count the occurrences of each distinguishable symbol in the string (use an array).Divide the factorial of the length by the product of the factorials of the occurrences.Return the quotient as the integer result.The point of this function is to find the result only by using the formulas for permutations.For example, BAAA the function counts that the string length is 4, that A occurs 3times, and that B occurs 1time, then divides 4!by 3!and 1!and returns the quotient, which is 4.

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

JAVA CODE: Main.java

import java.util.*;
public class Main
{
static int totalPermutations(String str)
{
int n=str.length();
int arr[]=new int[26];
for(int i=0;i<n;i++)
{
if(Character.isLowerCase(str.charAt(i)))
arr[str.charAt(i)-'a']++;
else if(Character.isUpperCase(str.charAt(i)))
arr[str.charAt(i)-'A']++;
}
int p=fact(n);
for(int i=0;i<26;i++)
p/=fact(arr[i]);
return p;
}
static int fact(int n)
{
if(n==1||n==0)
return 1;
return n*fact(n-1);
}
   public static void main(String[] args) {
       System.out.println("Total Permutations of string BAAA = "+totalPermutations("BAAA"));
       System.out.println("Total Permutations of string CACB = "+totalPermutations("CACB"));
       System.out.println("Total Permutations of string abcccc = "+totalPermutations("abcccc"));
   }
}

Output:

Total Permutations of string BAAA = 4 Total Permutations of string CACB = 12 Total Permutations of string abcccc = 30 Program

Add a comment
Know the answer?
Add Answer to:
WRITTEN IN JAVA Write a function as follows: Receive a string as a parameter.Count the length...
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
  • Write code in Java. In this step, functions and methods are synonymous. You may assume that...

    Write code in Java. In this step, functions and methods are synonymous. You may assume that strings only contain uppercase letters A through Z. 1. Write a function as follows: o Receive a string as a parameter. o Count the length of the string Count the occurrences of each distinguishable symbol in the string (use an array or map/dictionary). o Divide the factorial of the length by the product of the factorials of the occurrences. o Return the quotient as...

  • Java Counts the occurrences of each digit in a string

    Write a method that counts the occurrences of each digit in a string using the following header: public static int[] count(String s) The method counts how many times a digit appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example, after executing int[] counts = count("12203AB3"), counts[0] is 1, counts[1] is 1, counts[2] is 2, and counts[3] is 2. Write a test program that prompts the...

  • JAVA How do I write a code that receive a string, detect the length of each...

    JAVA How do I write a code that receive a string, detect the length of each token with array, save the frequency of the length and print it? If the token length is longer than 10, the length is considered to be 0. If the frequency of the lengtg is 0 the information will not be printed ex) If the input is "Count the length of the token tokenlongerthanten" the output will be Length0 : 1 Length2 : 1 Length3...

  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

  • Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon...

    Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int. B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1%...

  • Java: Write a program that prompts the user to enter integers in the range 1 to...

    Java: Write a program that prompts the user to enter integers in the range 1 to 50 and counts the occurrences of each integer. The program should also prompt the user for the number of integers that will be entered. As an example, if the user enters 10 integers (10, 20, 10, 30, 40, 49, 20, 10, 25, 10), the program output would be: 10 occurs 4 times 20 occurs 2 times 25 occurs 1 time 30 occurs 1 time...

  • 1. String Length Write a function that returns an integer and accepts a pointer to a...

    1. String Length Write a function that returns an integer and accepts a pointer to a C-string as an argument. The function should count the number of characters in the string and return that number. Demonstrate the function in a simple program that asks the user to input a string, passes it to the function, and then displays the function’s return value. c++

  • Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int)

    please do in c++Q4. Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int) that when passed a non-negative integer, returns the number of occurrences of the digit 7 in the number. So, for example: count7s(717) → 2 count75(7) →1 count7s(123) → 0 Q5. Write a C++ function of the form string mirrorEnds(string)that when given a string, looks for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or...

  • C++ Programming: Write a function that takes in a C++-style string s (from #include ), as...

    C++ Programming: Write a function that takes in a C++-style string s (from #include ), as well as an integer n, and then returns true if any substring of length n in s repeats itself, and false otherwise. For example, if s is "toe-to-toe" and n is 3, then the function would return true since "toe" occurs twice within s. However, if n were 4, the function would return false. As a second example, if s is "singing" and n...

  • I have written this function in C++. A string of integers is passed in as dString...

    I have written this function in C++. A string of integers is passed in as dString and an integer is passed in as count. each dString integer is then indexed and passed into strTolnt integer array. each strTolnt integer is then passed through an equation and stored in temp array. I have printed each index in the array and the correct values are displayed, but when I print the entire array the value is displayed as hex. temp array is...

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