Question

An online coin dealer offers bags of coins that are guaranteed to contain at least one full set. Given a string comprised of

Function Description Complete the function fewestCoins in the editor below. fewestCoins has the following parameter: string c

LANGUAGE: JAVA

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

java code:


import java.util.*;
import java.util.LinkedList;
import java.util.Queue;

public class Main
{
public boolean containALL(String str)
{ //check whether string contain all character (a,b,c,d) or not.
boolean flagA=false,flagB=false,flagC=false,flagD=false;
  
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='a')
flagA=true;
  
if(str.charAt(i)=='b')
flagB=true;
  
if(str.charAt(i)=='c')
flagC=true;
  
if(str.charAt(i)=='d')
flagD=true;
}
  
if(flagA&&flagB&&flagC&&flagD)
return true;
return false;
  
}
   public static void main(String[] args) {

       Main obj=new Main();
       Scanner sc=new Scanner(System.in);
       String input=sc.nextLine();
       int n=input.length(),min=1000000;
  
   Queue<Integer> q
= new LinkedList<>();
  
for(int i=0;i<n;i++)
{
if(input.charAt(i)=='a')
q.add(i); // all position of character 'a'.
}
Iterator value = q.iterator();
while (value.hasNext()) {
int index=q.peek();
  
for(int i=4;i<n;i++) //loop from min length i.e 4 to n.
{ //check all substring starting with character 'a'.
if(index+i<=n)
{
String substr2 = input.substring(index,index+i);
if(obj.containALL(substr2))
{
if(min>i-index+1)
{
min=i; //get the minimum length.
}
}
}
}
  
q.remove();
  
}
System.out.println(min);
   }
}

Output:

dabbcabcd 4

Add a comment
Know the answer?
Add Answer to:
LANGUAGE: JAVA An online coin dealer offers bags of coins that are guaranteed to contain at...
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 solve in Python. You would like to set a password for an email account. However,...

    Please solve in Python. You would like to set a password for an email account. However, there are two restrictions on the format of the password. It has to contain at least one uppercase character and it cannot contain any digits. You are given a string S consisting of N alphanumerical characters. You would like to find the longest substring of Sthat is a valid password. A substring is defined as a contiguous segment of a string. For example, given...

  • 3. A computer system uses passwords that contain exactly eight characters, and each character is one...

    3. A computer system uses passwords that contain exactly eight characters, and each character is one of the 26 lowercase letter (a -z) or 26 uppercase letters (A - Z) or 10 integers (0-9). Let Ω denote the set of all possible passwords. Suppose that all passwords in Ω are equally likely. Determine the probability for each of the following: (a) Password contains all lowercase letters given that it contains only letters (b) Password contains at least 1 uppercase letter...

  • A computer system uses passwords that contain exactly 3 characters, and each character is one of the 26 lowercase l...

    A computer system uses passwords that contain exactly 3 characters, and each character is one of the 26 lowercase letters (a-z) or 26 uppercase letters (A-Z) or 10 integers (0-9). Let Ω denote the set of all possible passwords, and let and denote the events that consist of passwords with only letters or only integers, respectively Suppose that all passwords in 2 are equally likely. Determine the following probabilities. Round your answers to three decimal places (e.g. 98.765). (c) The...

  • In java write a command-line program that helps to decrypt a message that has been encrypted...

    In java write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2). When your...

  • I need to construct a deterministic finite automata, DFA M, such that language of M, L(M),...

    I need to construct a deterministic finite automata, DFA M, such that language of M, L(M), is the set of all strings over the alphabet {a,b} in which every substring of length four has at least one b. Note: every substring with length less than four is in this language. For example, aba is in L(M) because there are no substrings of at least 4 so every substring of at least 4 contains at least one b. abaaab is in...

  • Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc"....

    Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc". An alphabetically-ordered sequence of substrings of s would be {"a", "ab", "abc", "b", "bc", "c"}. If the sequence is reduced to only those substrings that start with a vowel and end with a consonant, the result is {"ab", "abc"}. The alphabetically first element in this reduced list is "ab", and the alphabetically last element is "abc". As a reminder: • Vowels: a, e, i,...

  • JAVA PROBLEM! PLEASE DO IT ASAP!! REALLY URGENT! PLEASE DO NOT POST INCOMPLETE OR INCORRECT CODE...

    JAVA PROBLEM! PLEASE DO IT ASAP!! REALLY URGENT! PLEASE DO NOT POST INCOMPLETE OR INCORRECT CODE Below is the program -- fill the code as per the instructions commented: //---------------------------------------------------------------------------------------------------------------------------------------------------------------// /* The idea of this HW is as follows : A password must meet special requirements, for instance , it has to be of specific length, contains at least 2 capital letters , 2 lowercase letters, 2 symbols and 2 digits. A customer is hiring you to create a class...

  • can you use the isspace() method in the code what do i add to the code...

    can you use the isspace() method in the code what do i add to the code if i want to make sure that extra spaces dont affect the output of the code elect sumatra medium roast VS sumatra medium roast Det dat HETTI CV Surface Providesearch coffee record description to search for at that it was not found in the file the first on has another space in it Table 8-1 Some string testing methods Method Description isalnum() Returns true...

  • 1 (continued) b. (20 points) Complete the function with the prototype and description below. void removeNonLetters...

    1 (continued) b. (20 points) Complete the function with the prototype and description below. void removeNonLetters (char estr); This function should remove all characters in string str except letters. Remember that, since the string is passed by address, changes made to str inside the function are seen outside the function. For example, if the string 1. ERCE.2160 Fall 2019" before calling this function, then after calling removeNonLetters (1). 51 - "EECEFall". Hint: to access part of a string (a substring)...

  • Java 8 Braces You are designing a compiler for a C++ program and need to check...

    Java 8 Braces You are designing a compiler for a C++ program and need to check that braces in any given file are balanced Braces in a string are considered to be balanced if the following criteria are met: All braces must be closed. Braces come in pairs of the form 0.0andl1. The left brace opens the pair, and the right one closes it In any set of nested braces, the braces between any pair must be closed For example,...

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