Java Basics Part Two -16 Problems with Solutions

Problem-1
Given a string and a non-negative int n, return a larger string that is n copies of the original string.
Example:
stringTimes(“Hi”, 2) → “HiHi”
stringTimes(“Hi”, 3) → “HiHiHi”
stringTimes(“Hi”, 1) → “Hi”
Solution
public String stringTimes(String str, int n) {
String y=””; // empty string to store the result
for (int i=0;i<n; i++){
y=y+str;
}
return y;
}
Problem-2
Given a string and a non-negative int n, 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;
Example:
frontTimes(“Chocolate”, 2) → “ChoCho”
frontTimes(“Chocolate”, 3) → “ChoChoCho”
frontTimes(“Abc”, 3) → “AbcAbcAbc”
Solution
public String frontTimes(String str, int n) {
String result=””;
for(int i=0;i<n;i++){
if(str.length()<=3) result+=str;
if(str.length()>3) result+=str.substring(0,3);
}
return result;
}
Problem-3 
Count the number of “xx” in the given string. We’ll say that overlapping is allowed, so “xxx” contains 2 “xx”.
Example:
countXX(“abcxx”) → 1
countXX(“xxx”) → 2
countXX(“xxxx”) → 3
Solution
int countXX(String str) {
int count=0;
for(int i=0; i<str.length()-1;i++){
if(str.charAt(i)==’x’ && str.charAt(i+1)==’x’)
count++;
}
return count;
}
Problem-4
Given a string, return true if the first instance of “x” in the string is immediately followed by another “x”.
Example
doubleX(“axxbb”) → true
doubleX(“axaxax”) → false
doubleX(“xxxxx”) → true
Solution
boolean doubleX(String str) {
int e=str.indexOf(‘x’); // check the position of the first occurrence of ‘x’
if (e==-1) return false;  // if there is no x return false
if(e+1>=str.length()) return false; //if the first ‘x’ is at the end return false
return (str.substring(e,e+2).equals(“xx”));
}
Another Solution
boolean doubleX(String str) {
int e=str.indexOf(‘x’);
if (e==-1) return false;
if(e+1>=str.length()) return false;
String f=str.substring(e,e+2); // create a substring starting from the first ‘x’
return (f.startsWith(“xx”)); // compare the  substring f with “xx”
}
Problem -5
Given a non-empty string like “Code” return a string like “CCoCodCode”.
Example:
stringSplosion(“Code”) → “CCoCodCode”
stringSplosion(“abc”) → “aababc”
stringSplosion(“ab”) → “aab”
Solution
public String stringSplosion(String str) {
String new1=””;
for (int i=0;i<str.length();i++){
new1+=str.substring(0,i+1);
}
return new1;
}
Problem -6
Given a string, 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).
Example:
last2(“hixxhi”) → 1
last2(“xaxxaxaxx”) → 1
last2(“axxxaaxx”) → 2
Solution
public int last2(String str) {
if(str.length()<2) return 0;
int count=0;
for (int i=0;i<str.length()-1;i++){
if(str.charAt(i)==str.charAt(str.length()-2) && str.charAt(i+1)==str.charAt(str.length()-1)) count++;
}
return count-1;
}
Solution Two (better solution use .equals())
public int last2(String str) {
if(str.length()<2) return 0;
int count=0;
String fin=str.substring(str.length()-2);//create a substring of the final two chars
for (int i=0;i<str.length()-2;i++){
String new1=str.substring(i,i+2);
if(new1.equals(fin)) count++;// compare using  .equals()
}
return count;
}
Problem-7
Given an array of ints, return the number of 9’s in the array.
Example:
arrayCount9({1, 2, 9}) → 1
arrayCount9({1, 9, 9}) → 2
arrayCount9({1, 9, 9, 3, 9}) → 3
Solution
public int arrayCount9(int[] nums) {
int count=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==9) count++;
}
return count;
}
Problem-8
Given an array of ints, return true if one of the first 4 elements in the array is a 9. The array length may be less than 4.
Example:
arrayFront9({1, 2, 9, 3, 4}) → true
arrayFront9({1, 2, 3, 4, 9}) → false
arrayFront9({1, 2, 3, 4, 5}) → false
Solution
public boolean arrayFront9(int[] nums) {
int four=nums.length;
if (four>4) four=4; // limit the array length to be four
for (int i=0;i<four;i++){
if (nums[i]==9) return true;
}
return false;
}
Problem-9
Given an array of ints, return true if .. 1, 2, 3, .. appears in the array somewhere.
Example
array123({1, 1, 2, 3, 1}) → true
array123({1, 1, 2, 4, 1}) → false
array123({1, 1, 2, 1, 2, 3}) → true
Solution
public boolean array123(int[] nums) {
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;
}
Problem-10
Given 2 strings, a and b, 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.
Example
stringMatch(“xxcaazz”, “xxbaaz”) → 3
stringMatch(“abc”, “abc”) → 2
stringMatch(“abc”, “axc”) → 0
Solution
public int stringMatch(String a, String b) {
int count=0;
int r =Math.min(a.length(),b.length());
for(int i=0 ;i<r-1;i++){
String a1=a.substring(i,i+2);
String b2=b.substring(i,i+2);
if(a1.equals(b2)) count++;
}
return count;
}
Problem-11
Given a string, return a version where all the “x” have been removed. Except an “x” at the very start or end should not be removed.
Example:
stringX(“xxHxix”) → “xHix”
stringX(“abxxxcd”) → “abcd”
stringX(“xabxxxcdx”) → “xabcdx”
Solution
public String stringX(String str) {
String result=””;
for(int i=0;i<str.length();i++){
if(str.charAt(i)==’x’ && i!=0 && i!=str.length()-1)
continue; //if the x isn’t first or last jump 
result+=str.charAt(i);
}
return result;
}
Solution Two

public String stringX(String str) {
String result=””;
for(int i=0;i<str.length();i++){
if(str.substring(i,i+1).equals(“x”) && i!=0 && i!=str.length()-1)
continue;
result+=str.substring(i,i+1);
}
return result;
}
Problem-12
Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9 … so “kittens” yields “kien”.
Example:
altPairs(“kitten”) → “kien”
altPairs(“Chocolate”) → “Chole”
altPairs(“CodingHorror”) → “Congrr”
Solution
public String altPairs(String str) {
String result=””;
for(int i=0;i<str.length();i+=4){
int lim=i+2;
if(lim>str.length()){
lim=str.length();
}
result+=str.substring(i,lim);
}
return result;
}

Problem-13
Suppose the string “yak” is unlucky. Given a string, return a version where all the “yak” are removed, but the “a” can be any char. The “yak” strings will not overlap.
Example
stringYak(“yakpak”) → “pak”
stringYak(“pakyak”) → “pak”
stringYak(“yak123ya”) → “123ya”
Solution
public String stringYak(String str) {
String result=””;
for(int i=0; i<str.length();i++){
if(i+2< str.length() && str.charAt(i)==’y’ && str.charAt(i+2)==’k’){
i=i+2; // jump by 2 if there is a y-k 
}
else{
result+=str.charAt(i); 
}
}
return result;
}
Problem-14
Given an array of ints, 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.
Example:
array667({6, 6, 2}) → 1
array667({6, 6, 2, 6}) → 1
array667({6, 7, 2, 6}) → 1
Solution
public int array667(int[] nums) {
int count=0;
for(int i=0;i<nums.length-1;i++){
if (nums[i]==6 && (nums[i+1]==6 || nums[i+1]==7)) count++;
}
return count;
}
Problem-15
Given an array of ints, 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.
Example
noTriples({1, 1, 2, 2, 1}) → true
noTriples({1, 1, 2, 2, 2, 1}) → false
noTriples({1, 1, 1, 2, 2, 2, 1}) → false
Solution
public boolean noTriples(int[] nums) {
for(int i=0;i<nums.length-2;i++){
if (nums[i]==nums[i+1]&& nums[i+1]==nums[i+2]) return false;
}
return true;
}
Problem-16
Given an array of ints, 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.
Example
has271({1, 2, 7, 1}) → true
has271({1, 2, 8, 1}) → false
has271({2, 7, 1}) → true
Solution
public boolean has271(int[] nums) {
for (int i=0;i<nums.length-2;i++){
int k=nums[i];
int j=nums[i+2];
if(nums[i+1]==k+5 && (Math.abs(j-k+1)<=2)) return true;
}
return false;
}
**All Problems were taken from the website CodingBat