Solved

startMatch challenge

Posted on 2015-01-15
14
93 Views
Last Modified: 2015-02-05
Hi,

I am trying below challenge
http://codingbat.com/prob/p141494
I did not understand the challenge. please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 167 total points
ID: 40552570
Given a string and a second "word" string, we'll say that the word matches the string if it appears at the front of the string, except its first char does not need to match exactly. On a match, return the front of the string, or otherwise return the empty string. So, so with the string "hippo" the word "hi" returns "hi" and "xip" returns "hip". The word will be at least length 1.

startWord("hippo", "hi") → "hi"
startWord("hippo", "xip") → "hip"
startWord("hippo", "i") → "h"
"hi" matches the front of "hippo" exactly.
"xip" matches the front of "hippo", except that the first character does not match exactly
"i" matches the front of "hippo", except that the first character does not match exactly
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 167 total points
ID: 40553778
If a match of the "word" occurs at the beginning of the string "str" without respect to the first character in the string, return the substring of str for the length of the word. That infers that if the length of the word is one, the first character of the str will always be returned. What the challenge doesn't seem to address is what to return if there is no match at all. I suspect the answer to that would be to return an empty string. If that is the case, the following should work:

public static String startWord(String str, String word) {
	int len = word.length();
	if (len == 1) {
		return str.substring(0, 1);
	}
	String wordPart = word.substring(1);
	if (str.startsWith(wordPart, 1)) {
		return str.substring(0, len);
	}
	return "";
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40554474
Commenting out lines 3,4,5 of http:#a40553778 improves the result when str.length()==0
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 37

Accepted Solution

by:
zzynx earned 166 total points
ID: 40559302
Remember to first handle the "corner cases"?

Here we go.
1) if the string is empty, then just return the empty string

  if (str.isEmpty()) {
     return "";
  }

Open in new window

2) if the word completely fits the 'str' parameter, then return the word [startWord("hippo", "hi") -> "hi"]

  if (str.startsWith(word)) {
     return word;
  }

Open in new window

3) if the word (not taking into account the first character of word) fits the 'str' parameter, then return the part of 'str' that matches [startWord("hippo", "xip") -> "hip"]

  if (str.substring(1).startsWith(word.substring(1))){
     return str.substring(0, word.length());
  }

Open in new window

4) if the word fits the 'str' parameter (not taking into account the first character of str), then return the part of 'str' that matches [startWord("hippo", "i") -> "h"]

  if (str.substring(1).startsWith(word)){
     return str.substring(0, word.length());
  }

Open in new window


5) if none of the above, then return the empty string

  return "";

Open in new window



Bringing all this cases together we have as a result:

public String startWord(String str, String word) {
  if (str.isEmpty()) return "";
  if (str.startsWith(word)) {
     return word;
  }
  if (str.substring(1).startsWith(word.substring(1))){
     return str.substring(0, word.length());
  }
  if (str.substring(1).startsWith(word)){
     return str.substring(0, word.length());
  }
  return "";
}

Open in new window


Or when combining 3) and 4) together:

public String startWord(String str, String word) {
  if (str.isEmpty()) return "";
  if (str.startsWith(word)) {
     return word;
  }
  if ( str.substring(1).startsWith(word.substring(1)) || str.substring(1).startsWith(word) ){
     return str.substring(0, word.length());
  }
  return "";
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40559363
The code in http:#a40559302 also includes parts that are better commented out.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40559799
On a match, return the front of the string,

how many characters of the front of the string. Is i as many characters of the second "word" string. please advise
0
 
LVL 32

Expert Comment

by:awking00
ID: 40559885
If the word is made up of 2 characters and you have a match return the first 2 characters of the string, if word is 3 characters long return the first 3 characters of the string if you have a match, etc.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40559998
My code passes all tests
0
 
LVL 84

Expert Comment

by:ozo
ID: 40560030
The code in http:#a40559302 does pass all http://codingbat.com/prob/p141494 tests.
If part of the code was omitted, it would still pass all tests, and would also not fail to follow the English specification for some cases that  http://codingbat.com/prob/p141494 does not test.


Return the same front of the string that matched the second "word" string, which would contain the same number of characters.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40570725
"hi" matches the front of "hippo" exactly.

one general interest question how do you underline under "hi" as you did in comment

http:#a40552570
http://www.experts-exchange.com/Programming/Languages/Java/Q_28597724.html#a40552570

I tried to underline in word document and copy pasted and posted and underline is lost. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40570741
Do you see a U icon at the top of the "Post a Comment" window?
It wraps a
[u][/u]

Open in new window

around the selected text.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40570751
In the text box you are typing your comment, you
1) select the text you want to underline
2) you press the button with the underlined U as icon (cf. screenshot)
screenshot
The same way you can set text in bold or italic (using the buttons on the left of the U button)
You can probably guess what the other buttons do.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40570786
got it.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40592734
public String startWord(String str, String word) {
    if (str.isEmpty()) return "";
  if (str.startsWith(word)) {
     return word;
  }
  if ( str.substring(1).startsWith(word.substring(1)) /* || str.substring(1).startsWith(word) */ ){
     return str.substring(0, word.length());
  }
  return "";
}
passes all tests (but the  || str.substring(1).startsWith(word)  would cause startWord("hippo", "ipp") to incorrectly return "hip")
 public String startWord(String str, String word) {
  if ( str.length() >= word.length() && str.substring(1).startsWith(word.substring(1)) ){
     return str.substring(0, word.length());
  }
  return "";
}
passes all tests even more simply
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which non-HTML GUI front end to use with Java? 3 52
CSV file parsing thru Java 13 39
Java Eclipse Loop 3 29
netbeans can not run self signed app 8 9
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question