Question

Given a string S that contains lowercase English letters representing different types of candies. A substring...

Given a string S that contains lowercase English letters representing different types of

candies. A substring of a string S is a string Ssub that occurs in S. For example, "twix" is a

candy name which is the substring of "twtwixtwixnerdstwixnerds". Each candy costs 1 unit.

You can pick some consecutive candies such that you can create a palindrome of length L by

using some or all picked candies while the number of all possible cases K (all possible

palindromes) is given. The main goal is to find the minimum cost to create a palindrome of

length L. For example,

S= twtwixtwixnerdstwixnerds

L=8

K=4

P= twixtwix

Your task is to identify and define all possible inputs and outputs of a such given problem.

Use Java language.

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

import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;
// Program to find the palindrome name of the candies in a String
//string: twtwixtwixnerdstwixnerds

public class Palindrome {

    // Function will return palindrome
    // sub-string to be added
   private static void searchPalindromeCandies(String str,int L) {
        String s1 = "",s2="";
        int N = str.length(), count = 0;
      
       // to hold the pallindrome strings
        Set<String> palindromeArray = new HashSet<String>();
      
        System.out.println("Given string : " + str);
        System.out.println("\n******** Substring palindrome *******");
      
       //calculate the mid of the substring
       int mid=(L+1)/2;
       int minCost=0;
       boolean isFirstPallindromGot=false;
  
            for (int j = 0; j <= N-mid; j++) {
              
                s1 = str.substring(j, j+mid);
               s2=s1+s1;
              
               //count the min cost to find the first palindrome
               if(!isFirstPallindromGot)
                   minCost++;
              
                if (str.contains(s2)) {
                    palindromeArray.add(s2);
                   isFirstPallindromGot=true;
                  
                }
            }

       //print all palindrome strings
        System.out.println(palindromeArray);
      
        for (String s : palindromeArray)
            System.out.println(s + " - sub-string is a palindrome.");
        System.out.println("The no.of palindrome are: "+ palindromeArray.size());
       System.out.println("The Min. Cost : "+minCost);
      
    }
  
    // Driver program
    public static void main(String[] args)
    {
      
       Scanner sc=new Scanner(System.in);
      
       //user string input
       System.out.print("Enter Any String: ");
       String str = sc.next();
      
       //Length of Palindrome (two times of candies name)
       System.out.print("Enter Length Of Pallindrome: ");
       int L=sc.nextInt();
       searchPalindromeCandies(str,L);
    }
}

Add a comment
Know the answer?
Add Answer to:
Given a string S that contains lowercase English letters representing different types of candies. A substring...
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
  • Palindromes A palindrome is a nonempty string over some alphabet that reads the same forward and...

    Palindromes A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, civic, racecar, noon, and aibohphobia (fear of palindromes). You may assume that in the problems below, an input string is given as an array of characters. For example, input string noon is given as an array s[L.4], where s[1] = n, s[2] = o, s[3] = o, and s[4] = n. (a) (15 pts)...

  • Programming language: Java m: substring length n: input strings d: Hamming distance (mismatch) I: Number of...

    Programming language: Java m: substring length n: input strings d: Hamming distance (mismatch) I: Number of letters in Sample input string (s1, s2, s3) Strings consists of: A, C, G, and T Example outputs: Generate all possible possibilities of length m(4) using the values A, C, G, and T. EX possibilites: {A,A,A,A A,A,A,C.... G,G,G,G} and find all the possible combinations that have the same sequence with a hamming distance of 1 (only 1 difference) Given n input strings of length...

  • Given a str s, create a new str variable out that contains all of the letters...

    Given a str s, create a new str variable out that contains all of the letters in s that come after "j" in the alphabet, in their original order (hint: use the > operator). For example, if s is "hello", out should contain "llo". If s is "onmlkjih", out should contain "onmlk". If s is "abc", out should contain "" (i.e., an empty string). In Python

  • 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) *...

  • A CERTAIN program to user's password containing rules such as least n length, one uppercase, lowercase,...

    A CERTAIN program to user's password containing rules such as least n length, one uppercase, lowercase, one digit and white space is given as the code down below. So, simiiar to those rules, I need the code for the following questions post in pictures such as no more three consecutive letters of English alphabets, password should not contain User name and so on. //GOAL: To learn how to create that make strong passwords //Purpose: 1)To learn some rules that makes...

  • You will be given several strings full of different keyboard characters, some of which are letters...

    You will be given several strings full of different keyboard characters, some of which are letters of the alphabet. Write a java program that creates a binary tree that uses only the alphabetical characters (a-z, A-Z) as the value within the leaf nodes using recursion and preorder traversal. All other values within the tree (either the root node or internal nodes) will be null. You may create any methods you see fit, so long as the final binary tree is...

  • Create C++ program.Convert this string to Tap Code using structures. It is very important to use structures in this program. The tap code is based on a Polybius square using a 5×5 grid of letters repr...

    Create C++ program.Convert this string to Tap Code using structures. It is very important to use structures in this program. The tap code is based on a Polybius square using a 5×5 grid of letters representing all the letters of the Latin alphabet, except for K, which is represented by C. The listener only needs to discriminate the timing of the taps to isolate letters. Each letter is communicated by tapping two numbers the first designating the row (Down) the...

  • Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers...

    Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers (the "backwards" cipher, the Rail Fence cipher, and the Column Transposition cipher) where the ciphertext is created via an altered presentation of the plaintext. The algorithm for each is detailed in the function descriptions in this section. (13 points) def backwards_cipher(plaintext, key): • Parameter(s): plaintext ----- a string; the message to be encrypted key ----- an integer; the number to control this cipher •...

  • Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...

  • Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in...

    Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in which players construct words from random letters, building on words already played. Each letter has an associated point value and the aim is to collect more points than your opponent. Please see https: //en.wikipedia.org/wiki/Scrabble for an overview if you are unfamiliar with the game. You will write a program that allows a user to enter 7 letters (representing the letter tiles they hold), plus...

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