This post contains the solutions to Java String-1 section of codingbat.com for problems 26 to 33. If you are new here, you can check my previous post learn-coding-computer-programming-for-beginners. Solutions to problems 1 to 18 are available in the link java-string1-part-1, java-string1-part2.Solutions to previous sections are also available check the links to java-warmup-1 , java-warmup-2.
26. Problem Name : frontAgain
Solution:
Hint:
Given a string, return true if the first 2 chars in the string also appear at
the end of the string, such as with "edited".
public boolean frontAgain(String
str)
{
if(str.length()>2)
{
if(str.substring(0,2).equals(str.substring(str.length()-2,str.length())))
return true;
else
return false;
}
else if(str.length()==2)
return true;
else
return false;
}
|
27. Problem Name : minCat
Solution:
Hint: Given two strings, append them together (known as
"concatenation") and return the result
Constraint:
However, if the strings are different lengths, omit chars from the longer
string so it is the same length as the shorter string. So "Hello" and
"Hi" yield "loHi". The strings may be any length.
public String
minCat(String a, String b)
{
if(a.length()>b.length())
return(a.substring(a.length()-b.length(),a.length())+b);
else
return(a+b.substring(b.length()-a.length(),b.length()));
}
|
28. Problem Name : extraFront
Solution:
Hint: Given a string, return a new string made of 3 copies of the first 2
chars of the original string.
Constraint: The string may be any length. If there are fewer than 2
chars, use whatever is there.
public String extraFront(String str)
{
String result="";int i=0;
if(str.length()>2)
{
for(i=0;i<3;i++)
result+=str.substring(0,2);
}
else
{
for(i=0;i<3;i++)
result+=str.substring(0,str.length());
}
return result;
}
|
29. Problem Name : without2
Solution:
Hint: Given a string, if a length 2 substring appears at both its
beginning and end, return a string without the substring at the beginning, so
"HelloHe" yields "lloHe".
Constraint:
The substring may overlap with itself, so "Hi" yields "".
Otherwise, return the original string unchanged.
public String
without2(String str)
{
int len=str.length();
if(str.length()>2)
{
if(str.substring(0,2).equals(str.substring(len-2,len)))
return(str.substring(2,len));
else
return str;
}
else if(str.length()==1)
return str;
else
return "";
}
|
30. Problem Name : deFront
Solution:
Hint: Given a string, return a version without the first 2 chars. except
keep the first char if it is 'a' and keep the second char if it is 'b'.
Constraint:
The string may be any length.
public String
deFront(String str)
{
int len=str.length();
int flag=0;
String result="";
if(str.charAt(1)=='b')
{
result=(str.charAt(1)+str.substring(2,len));
flag=1;
}
if(str.charAt(0)=='a')
{
if(flag==1)
{
result=(str.charAt(0)+result);
}
else
{
result=str.charAt(0)+str.substring(2,len);
}
flag=1;
}
if(flag!=1)
{
result= str.substring(2,len);
}
return result;
}
|
31. Problem Name : startWord
Solution:
Hint: 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.
Constraint:
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.
public String
startWord(String str, String word)
{
int len=word.length();
if(str.length()==0)
return new String("");
if(len==1 ){
return str.substring(0,
1);
}
String str1="";
if(str.length()>=word.length())
{
str1=str.substring(1,len);
}
else
{
str1="";
}
word=word.substring(1);
if(word.equals(str1))
return str.substring(0,
len);
else
return new String("");
}
|
32. Problem Name : withoutX
Solution:
Hint:
Given a string, if the first or last chars are 'x', return the string without
those 'x' chars, and otherwise return the string unchanged.
public String
withoutX(String str)
{
int len=str.length();
String result="";
if(len>2)
{
if(str.charAt(0)=='x' &&
str.charAt(len-1)=='x')
result=str.substring(1,len-1);
else if(str.charAt(0)=='x' ||
str.charAt(len-1)=='x')
{
if(str.charAt(0)=='x')
result=str.substring(1,len);
if(str.charAt(len-1)=='x')
result=str.substring(0,len-1);
}
else
result=str;
}
else
{
if(!str.contains("x"))
result=str;
}
return result;
}
|
33. Problem Name : withoutX2
Solution:
Hint:
Given a string, if one or both of the first 2 chars is 'x', return the string
without those 'x' chars, and otherwise return the string unchanged.
public String
withoutX2(String str)
{
int len=str.length();
String result="";
if(str.length()>1)
{
if(str.charAt(0)=='x' &&
str.charAt(1)=='x')
{
str=str.substring(1,1)+str.substring(2,str.length());
result=str;
}
else if(str.charAt(0)=='x')
{
str=str.substring(1,2)+str.substring(2,str.length());
result=str;
}
else if(str.charAt(1)=='x')
{
str=str.substring(0,1)+str.substring(2,str.length());
result=str;
}
else
{
result=str;
}
}
else
{
result="";
}
return result;
}
|
No comments:
Post a Comment