This post contains the solutions to Java Warm up-2 section
of codingbat.com. If you are new here,
you can check my previous post learn-coding-computer-programming-for-beginners. If you are looking for Java Warm up 1 section you
can visit codingbat-java-warmup-1-solutions-1-to-15.
1. Problem Name : stringTimes
Solution:
Hint:
return a larger string that is n copies of the original string
public String
stringTimes(String str, int n) {
String result="";
for(int i=0;i<n;i++)
{
result=result+str;
}
return result;
}
|
2. Problem Name : frontTimes
Solution:
Hint:
we'll say that the front of the string is the first 3 chars, or whatever is
there if the string is less than length 3. Return n copies of the front.
public String frontTimes(String str, int n) {
int frontLen = 3;
if (frontLen > str.length()) {
frontLen = str.length();
}
String front = str.substring(0, frontLen);
String result = "";
for (int i=0; i<n; i++) {
result = result + front;
}
return result;
}
|
3. Problem Name : countXX
Solution:
Hint:
Count the number of "xx" in the given string with overlapping allowed,
("xxx" contains 2 "xx")
public int countXX(String
str) {
int count = 0;
for (int i = 0; i < str.length()-1; i++) {
if (str.substring(i, i+2).equals("xx")) count++;
}
return count;
}
|
4. Problem Name : sumDouble
Solution:
Hint:
return true if the first instance of
"x" in the string is immediately followed by another "x")
boolean doubleX(String
str) {
int i = str.indexOf("x");
if (i == -1) return false; // no "x" at all
//
Is char at i+1 also an "x"?
if (i+1 >= str.length()) return false; // check i+1 in
bounds?
return str.substring(i+1, i+2).equals("x");
//
Another approach -- .startsWith() simplifies the logic
//
String x = str.substring(i);
//
return x.startsWith("xx");
}
|
5. Problem Name : stringBits
Solution:
Hint: return a new string made of every other
char starting with the first, so "Hello" yields "Hlo".
public String
stringBits(String str) {
String result = "";
//
Note: the loop increments i by 2
for (int i=0; i<str.length(); i+=2) {
result = result + str.substring(i,
i+1);
// Alternately could use str.charAt(i)
}
return result;
}
|
6. Problem Name : stringSplosion
Solution:
Hint:
Given a non-empty string like "Code" return a string like
"CCoCodCode
public String
stringSplosion(String str) {
int len=str.length();
int i=0,j=0;
String result="";
for (i=0;i<=len;i++)
{
for(j=0;j<i;j++)
{
result=result+str.charAt(j);
}
}
return result;
}
|
7. Problem Name : last2
Solution:
Hint: return the count
of the number of times that a substring length 2 appears in the string and also
as the last 2 chars of the string, so "hixxxhi" yields 1 (we won't
count the end substring).
public int last2(String str)
{
//
Screen out too-short string case.
if (str.length() < 2) return 0;
String end = str.substring(str.length()-2);
//
Note: substring() with 1 value goes through the end of the string
int count = 0;
//
Check each substring length 2 starting at i
for (int i=0; i<str.length()-2; i++) {
String sub = str.substring(i, i+2);
if (sub.equals(end)) { // Use .equals() with strings
count++;
}
}
return count;
}
|
8. Problem Name : arrayCount9
Solution:
Hint: return the number of 9's in the array
public int arrayCount9(int[] nums) {
int len=nums.length;
int counter=0,i=0;
for(i=0;i<len;i++)
{
if(nums[i]==9)
{
counter++;
}
}
return counter;
}
|
9. Problem Name : arrayFront9
Solution:
Hint: Return true if
one of the first 4 elements in the array is a 9. With constraint the array
length may be less than 4
public boolean arrayFront9(int[] nums) {
//
First figure the end for the loop
int end = nums.length;
if (end > 4) end = 4;
for (int i=0; i<end; i++) {
if (nums[i] == 9) return true;
}
return false;
}
|
10. Problem Name : array123
Solution:
Hint: return true if 1, 2, 3, .. appears in the
array somewhere.
public boolean array123(int[] nums) {
//
Note: iterate < length-2, so can use i+1 and i+2 in the loop
for (int i=0; i < (nums.length-2); i++) {
if (nums[i]==1 && nums[i+1]==2
&& nums[i+2]==3) return true;
}
return false;
}
|
11. Problem Name : stringMatch
Solution:
Hint: return the number of the positions where they
contain the same length 2 substring. So "xxcaazz" and
"xxbaaz" yields 3, since the "xx", "aa", and
"az" substrings appear in the same place in both strings
public int stringMatch(String
a, String b) {
//
Figure which string is shorter.
int len = Math.min(a.length(), b.length());
int count = 0;
//
Look at both substrings starting at i
for (int i=0; i<len-1; i++) {
String aSub = a.substring(i, i+2);
String bSub = b.substring(i, i+2);
if (aSub.equals(bSub)) { // Use .equals() with strings
count++;
}
}
return count;
}
|
12. Problem Name : stringX
Solution:
Hint:
return a version where the entire "x" has been removed. With constraint:
Except an "x" at the very start or end should not be removed
public String
stringX(String str) {
String result = "";
for (int i=0; i<str.length(); i++) {
// Only append the char if it is not the
"x" case
if (!(i > 0 && i <
(str.length()-1) && str.substring(i, i+1).equals("x"))) {
result = result +
str.substring(i, i+1); // Could use str.charAt(i) here
}
}
return result;
}
|
13. Problem Name : altPairs
Solution:
Hint:
return a string made of the chars at indexes 0, 1, 4, 5, 8, 9 ... so
"kittens" yields "kien"
public String altPairs(String str) {
String result = "";
//
Run i by 4 to hit 0, 4, 8, ...
for (int i=0; i<str.length(); i += 4) {
// Append the chars between i and i+2
int end = i + 2;
if (end > str.length()) {
end = str.length();
}
result = result + str.substring(i,
end);
}
return result;
}
|
14. Problem Name : stringYak
Solution:
Hint:
Given a string, return a version where the entire "yak" is removed,
but the "a" can be any char. With constraint the "yak"
strings will not overlap.
public String stringYak(String str) {
String result = "";
for (int i=0; i<str.length(); i++) {
// Look for i starting a "yak"
-- advance i in that case
if (i+2<str.length() &&
str.charAt(i)=='y' && str.charAt(i+2)=='k') {
i =
i + 2;
} else { // Otherwise do the normal append
result = result + str.charAt(i);
}
}
return result;
}
|
15. Problem Name : array667
Solution:
Hint:
return the number of times that two 6's are next to each other in the array. Also
count instances where the second "6" is actually a 7.
public int array667(int[] nums) {
int count = 0;
//
Note: iterate to length-1, so can use i+1 in the loop
for (int i=0; i < (nums.length-1); i++) {
if (nums[i] == 6) {
if (nums[i+1] == 6 || nums[i+1] == 7) {
count++;
}
}
}
return count;
}
|
16. Problem Name : noTriples
Solution:
Hint:
we'll say that a triple is a value appearing 3 times in a row in the array.
Return true if the array does not contain any triples.
public boolean noTriples(int[] nums) {
//
Iterate < length-2, so can use i+1 and i+2 in the loop.
//
Return false immediately if every seeing a triple.
for (int i=0; i < (nums.length-2); i++) {
int first = nums[i];
if (nums[i+1]==first &&
nums[i+2]==first) return false;
}
//
If we get here ... no triples.
return true;
}
|
17. Problem Name : has271
Solution:
Hint: return true if
it contains a 2, 7, 1 pattern -- a value, followed by the value plus 5,
followed by the value minus 1. Additionally the 271 counts even if the
"1" differs by 2 or less from the correct value.
public boolean has271(int[] nums) {
int length=nums.length, i=0;
for(i=1;i<length;i++)
{
if((nums[i-1]+5==nums[i]) &&((nums[i-1]-1==nums[i+1])
||(nums[i-1]+1==nums[i+1])||(nums[i-1]-2==nums[i+1])||(nums[i-1]-3==nums[i+1])))
{
return true;
}
}
return false;
}
|
You
have completed the Java Warm up 2 section of codingbat.com.
Keep watching my posts for solutions to other sections.