Question

In: Computer Science

In java please Question: You are given a string s. Your task is to count the...

In java please Question: You are given a string s. Your task is to count the number of ways of splitting s into three non-empty parts a, b and c (s = a + b + c) in such a way that a + b, b + c and c + a are all different strings. For s = "xzxzx", the output should be countWaysToSplit(s) = 5. Consider all the ways to split s into three non-empty parts:

Solutions

Expert Solution

`Hey,

Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.

import java.util.ArrayList; // import the ArrayList class
public class Test{
public static int countWaysToSplit(String s,ArrayList<String> strs)
{
if(strs.size()>=3&&s.length()!=0)
return 0;
if(s.length()==0)
{
if(strs.size()<3)
return 0;
String a=strs.get(0);
String b=strs.get(1);
String c=strs.get(2);
String s1=a+b;
String s2=b+c;
String s3=c+a;
if(!s1.equals(s2)&&!s2.equals(s3)&&!s3.equals(s1))
{
return 1;
}
else
return 0;
}
int sum=0;
for(int i=0;i<s.length();i++)
{
strs.add(s.substring(0,i+1));
sum+=countWaysToSplit(s.substring(i+1,s.length()),strs);
strs.remove(strs.size()-1);
}
return sum;
}
public static void main(String []args){
String s = "xzxzx";
ArrayList<String> strs = new ArrayList<String>();
System.out.println(countWaysToSplit(s,strs));
}
}

Kindly revert for any queries

Thanks.


Related Solutions

Using Java please You are given an array of integers arr. Your task is to count...
Using Java please You are given an array of integers arr. Your task is to count the number of contiguous subarrays, such that each element of the subarray appears at least twice. E.g For arr = [0, 0, 0], the output should be duplicatesOnSegment(arr) = 3.
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
Please answer the following Question in 300 word count Please answer in your own Count. if...
Please answer the following Question in 300 word count Please answer in your own Count. if citing source please add reference at the end of question. You are the chief financial officer (CFO) at a community hospital. One of the comments that has come back from patient surveys is the need for a commercial 24-hour pharmacy within the hospital. In this way, patients or their families will be able to fill prescriptions and begin taking ordered medication right away instead...
Please answer the following Question in 300 word count Please answer in your own Count. if...
Please answer the following Question in 300 word count Please answer in your own Count. if citing source please add reference at the end of question. You are the chief financial officer (CFO) at a community hospital. One of the comments that has come back from patient surveys is the need for a commercial 24-hour pharmacy within the hospital. In this way, patients or their families will be able to fill prescriptions and begin taking ordered medication right away instead...
Please answer the following Question in 300 word count Please answer in your own Count. if...
Please answer the following Question in 300 word count Please answer in your own Count. if citing source please add reference at the end of question. You are the chief financial officer (CFO) at a community hospital. One of the comments that has come back from patient surveys is the need for a commercial 24-hour pharmacy within the hospital. In this way, patients or their families will be able to fill prescriptions and begin taking ordered medication right away instead...
C Programming Language Please Given a string S and keyword string K, encrypt S using keyword...
C Programming Language Please Given a string S and keyword string K, encrypt S using keyword Cipher algorithm. Note: The encryption only works on alphabets. Numbers and symbols such as 1 to 9, -, &, $ etc remain unencrypted. Input:     Zombie Here     secret     where: First line represents the unencrypted string S. Second line represents the keyword string K. Output:     ZLJEFT DTOT Explanation: We used "secret" keyword there. Plain Text: A B C D E F G H I J K...
In this program: ================================================================== /* Program to count number of occurrences of a given string in...
In this program: ================================================================== /* Program to count number of occurrences of a given string in original string*/ #include <iostream> #include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() { const int SIZE = 40; char str[SIZE]; char str1[SIZE]; char searchString[SIZE]; int n; int l1, l2; int count = 0; printf("Enter a sentence: \n"); fgets(str,SIZE,stdin); printf("Enter a search word: \n"); fgets(searchString,SIZE,stdin); if (str1[strlen(str1) - 1] == '\n') { str1[strlen(str1)-1] = '\0'; } if (str[strlen(str) - 1] == '\n')...
Write a java program to read a string from the keyboard, and count number of digits,...
Write a java program to read a string from the keyboard, and count number of digits, letters, and whitespaces on the entered string. You should name this project as Lab5B. This program asks user to enter string which contains following characters: letters or digits, or whitespaces. The length of the string should be more than 8. You should use nextLine() method to read string from keyboard. You need to extract each character, and check whether the character is a letter...
IF YOU HAVE ANSWERED THIS QUESTION BEFORE THEN PLEASE DONOT ANSWER AGAIN. Your task for this...
IF YOU HAVE ANSWERED THIS QUESTION BEFORE THEN PLEASE DONOT ANSWER AGAIN. Your task for this assignment is to implement a stack data structure in C++. This may be accomplished by utilizing the C++ standard template library (STL) or by utilizing a user-defined class. Implement a transaction-based stack data structure using C++. The program will be interactive. Data transactions will be entered at the command line and results will be displayed on the console. Each input transaction will contain an...
Given a string, such as x = ‘itm330’, write a Python program to count the number...
Given a string, such as x = ‘itm330’, write a Python program to count the number of digits and the number of letters in it. For example, in ‘itm330’, there are 3 letters and 3 digits. Hint: Very similar to page 11 on the slides. To check if a character c is a digit, use c.isdigit(). If c is a digit, c.isdigit() will be a True.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT