Question

Given the following method: public boolean check(String s) { return s.length() >= 2 && (s.charAt(0) ==...

Given the following method:

public boolean check(String s) {

return s.length() >= 2 &&

(s.charAt(0) == s.charAt(1) || check(s.substring(1))); }

This method will return true if and only if:

1. s.charAt(0) == s.charAt(1)

2. s starts with two or more of the same characters

3. s ends with two or more of the same characters

4. s contains two or more of the same characters

5. s contains two or more of the same character in a row

Homework Answers

Answer #1
Given
public boolean check(String s) {
    return s.length() >= 2 && (s.charAt(0) == s.charAt(1) || check(s.substring(1)));
}

s.length() >= 2 means string length should be greater than or equals to 2

s.charAt(0) == s.charAt(1) means the first two characters of the string are same ot

check(s.substring(1)) is a recursive call doing same from string except the first character.
 
So, (s.charAt(0) == s.charAt(1) || check(s.substring(1)) checks for character match for the to adjacent characters in the string.

So, answer is "s contains two or more of the same character in a row"

s contains two or more of the same character in a row

Know the answer?
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
Similar Questions
1. Suppose that the integer variables n and m have been initialized. Check all the correct...
1. Suppose that the integer variables n and m have been initialized. Check all the correct statements about the following code fragment. String s2 = (n>=m) ? "one" : ( (m<=2*n) ? "two": "three" ); Question 5 options: If m is strictly greater than 2n then the value of s2 is "two" If n is equal to m then the value of s2 is "two" If m is strictly greater than 2n then the value of s2 is "three" If...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) {...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) { String str = input.toLowerCase(); return bar(str); } private String bar(String s) { if (s.length() <= 1)   {return "";} else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));} else if (s.length() % 2 == 1) {return "*" + bar(s+"n");} else {return "";} } Which line(s) indicate the name of the helper method? Which line(s) contains recursive call(s)? How many * are in...
Considering the following code segment, answer the multiple choice questions. Which line(s) indicate the base case...
Considering the following code segment, answer the multiple choice questions. Which line(s) indicate the base case of the recursion? Which line(s) of code make recursion move toward the base case? public String foo(String input) { String str = input.toLowerCase(); return bar(str); } private String bar(String s) { if (s.length() <= 1)   {return "";} else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));} else if (s.length() % 2 == 1) {return "*" + bar(s+"n");} else {return "";} }
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine...
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. (b) What is the big-O complexity of your method in terms of the list size n.
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)          ...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)           return true;        else           return false; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length)        return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 2, 3, 3,...
You are given a string containing a sequence of open and close brackets. and isOpen, isClose,...
You are given a string containing a sequence of open and close brackets. and isOpen, isClose, isMatching methods as defined below. Write a recursive algorithm in pseudocode for the method isBalanced that takes the  string and a stack of characters as the input and checks if the given input string contains balanced brackets. For example "(][)" would be considered not balanced where "[()]" would be considered balanced. The recursive isBalanced method utilizes  isOpen, isClose, isMatching methods as defined below. static String open...
The code I have so far:    public static boolean validPass(String sPass)    {    boolean...
The code I have so far:    public static boolean validPass(String sPass)    {    boolean y=false;    if(sPass.length()>10)    {    int i,j=0,k=0;    char c;    for(i=0;i<sPass.length();i++)    {              c=sPass.charAt(i);    if(c>='A' && c<='Z')    {    j++;    }    else if(c>='a' && c<='z');    else if (!(sPass.contains("&") || sPass.contains("-")    || sPass.contains("%") || sPass.contains("_")    ||sPass.contains("^")||sPass.contains("@")));    else if(c>='0' && c<='9')    {    k++;    }    else    {   ...
This file that has the outline of an iterator which will be used to return each...
This file that has the outline of an iterator which will be used to return each char within a String. Complete the hasNext() and next() methods to enable this iteration. In case it is of use, String has two methods which may provide useful -- size() (returning the number of chars it contains) and charAt() (returning the character at the location passed as an argument). import java.util.Iterator; import java.util.NoSuchElementException; public class CharacterIterator implements Iterator<Character> { /** The String whose characters...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT