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...
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')...
Question for Java String[] tokens = expression.split(" "); for (String token : tokens) { if (token.equals("+")...
Question for Java String[] tokens = expression.split(" "); for (String token : tokens) { if (token.equals("+") || token.equals("-")) { while (!ops.isEmpty() && (ops.peek() == '+' || ops.peek() == '-' || ops.peek() == '*' || ops.peek() == '/')) {    applyOp(values, ops); } What does the for (String tokens : token) mean? What are the different ways to write this for loop?
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
Your task is to count the frequency of words in a text file, and return the...
Your task is to count the frequency of words in a text file, and return the most frequent word with its count. For example, given the following text: there are two ways of constructing a software design one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. Based on the example your program should printout the following along with the...
Please answer the following question in 250word count in your own word and thoughts please. Finance...
Please answer the following question in 250word count in your own word and thoughts please. Finance is the study of how businesses allocate money over time. With the implementation of the Affordable Care Act, it is posited that financial management will become more complicated. Do you agree or disagree? Explain the rationale for your response.
Please discuss the following question in your own word 300 word count on the following question....
Please discuss the following question in your own word 300 word count on the following question. Continuous process improvement is a significant step in Kotter's last step "make it stick". The last step defines the cultural change for the organization and is marked by employee buy-in, leadership's ability to motivate staff towards change, and redefining the organization's position on the "process change". As a future health care leader you create the vision and point your staff to the path towards...
PLEASE DO NOT OVERRIDE ANY EXCEPTIONS TASK: You want to develop a Java program that will...
PLEASE DO NOT OVERRIDE ANY EXCEPTIONS TASK: You want to develop a Java program that will allow you to keep track of a set of employees. In reviewing your employee list, you notice that your employees fall into two categories: Salaried and Hourly. The following table shows the information that you keep in your employee list for each type of employee. Field Type Salaried Hourly id int Yes Yes name String Yes Yes title String Yes No position String No...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT