Java Strings Part Two- 16 Problems with Solutions

Problem-1
Given a string, return a string where for every char in the original, there are two chars.
Example
doubleChar(“The”) → “TThhee”
doubleChar(“AAbb”) → “AAAAbbbb”
doubleChar(“Hi-There”) → “HHii–TThheerree”
Solution
public String doubleChar(String str) {
String result=””;
for (int i=0;i<str.length();i++){
result+=str.substring(i,i+1)+str.substring(i,i+1);
}
return result;
}
Problem-2
Return the number of times that the string “hi” appears anywhere in the given string.
Example
countHi(“abc hi ho”) → 1
countHi(“ABChi hi”) → 2
countHi(“hihi”) → 2
Solution
public int countHi(String str) {
int count=0;
for (int i=0;i<str.length();i++){
if(i+2>str.length()) continue;;
if (str.substring(i,i+2).equals(“hi”)) count++;
}
return count;
}
Problem-3
Return true if the string “cat” and “dog” appear the same number of times in the given string.
Example
catDog(“catdog”) → true
catDog(“catcat”) → false
catDog(“1cat1cadodog”) → true
Solution
public boolean catDog(String str) {
int count1=0;
int count2=0;
for (int i=0;i<str.length();i++){
if(i+3>str.length()) continue;
if (str.substring(i,i+3).equals(“cat”)) count1++;
}
for (int i=0;i<str.length();i++){
if(i+3>str.length()) continue;
if (str.substring(i,i+3).equals(“dog”)) count2++;
}
if (count1==count2) return true;
return false;
}
Problem-4
Return the number of times that the string “code” appears anywhere in the given string, except we’ll accept any letter for the ‘d’, so “cope” and “cooe” count.
Example
countCode(“aaacodebbb”) → 1
countCode(“codexxcode”) → 2
countCode(“cozexxcope”) → 2
Solution
public int countCode(String str) {
int count=0;
for(int i=0;i<str.length();i++){
if(i+4>str.length()) continue;
else if (str.substring(i,i+2).equals(“co”) && str.substring(i+3,i+4).equals(“e”)) count++;
}
return count;
}
Problem-5
Given two strings, return true if either of the strings appears at the very end of the other string, ignoring upper/lower case differences (in other words, the computation should not be “case sensitive”). Note: str.toLowerCase() returns the lowercase version of a string.
Example
endOther(“Hiabc”, “abc”) → true
endOther(“AbC”, “HiaBc”) → true
endOther(“abc”, “abXabc”) → true
Solution
public boolean endOther(String a, String b) {
String al=a.toLowerCase();
int alen=al.length();
String bl=b.toLowerCase();
int blen=bl.length();
if ((alen>=blen) && (bl.equals(al.substring(alen-blen))) || ((alen<blen) && ( al.equals(bl.substring(blen-alen))))) return true;
return false;
}
Problem-6
Return true if the given string contains a “bob” string, but where the middle ‘o’ char can be any char.
Example
bobThere(“abcbob”) → true
bobThere(“b9b”) → true
bobThere(“bac”) → false
Solution
public boolean bobThere(String str) {
for ( int i=0;i<str.length();i++){
if(i+2>str.length()-1) continue;
else if (str.charAt(i)==’b’ && str.charAt (i+2)==’b’) return true;
}
return false;
}
Problem-7
We’ll say that a String is xy-balanced if for all the ‘x’ chars in the string, there exists a ‘y’ char somewhere later in the string. So “xxy” is balanced, but “xyx” is not. One ‘y’ can balance multiple ‘x’s. Return true if the given string is xy-balanced.
Example
xyBalance(“aaxbby”) → true
xyBalance(“aaxbb”) → false
xyBalance(“yaaxbb”) → false
Solution
public boolean xyBalance(String str) {
int y=str.lastIndexOf(‘y’); // lastIndexOf returns the position of the last char ‘y’
int x=str.lastIndexOf(‘x’);
if ((x==-1 && y==-1) ||str.length()==0 || y>x) return true; // -1 means there isn’t any ‘x’ or ‘y’ 
return false;
}
Problem-8
Given two strings, a and b, create a bigger string made of the first char of a, the first char of b, the second char of a, the second char of b, and so on. Any leftover chars go at the end of the result.
Example
mixString(“abc”, “xyz”) → “axbycz”
mixString(“Hi”, “There”) → “HTihere”
mixString(“xxxx”, “There”) → “xTxhxexre”
Solution
public String mixString(String a, String b) {
String front= “”;
String result=””;
String remaining=””;
int limit=0;
int a1=a.length();
int b1=b.length();
if(a1==0) result=b;
if(b1==0) result=a;
if(a1<=b1) {
limit=a1;
remaining=b.substring(a1);
}
if(a1>b1){
limit=b1;
remaining= a.substring(b1);
}
for (int i=0;i<limit;i++){
front=front+a.charAt(i)+b.charAt(i) ;
result=front+remaining;
}
return result;
}
Problem-9
Given a string and an int n, return a string made of n repetitions of the last n characters of the string. You may assume that n is between 0 and the length of the string, inclusive.
Example
repeatEnd(“Hello”, 3) → “llollollo”
repeatEnd(“Hello”, 2) → “lolo”
repeatEnd(“Hello”, 1) → “o”
Solution
public String repeatEnd(String str, int n) {
int k=str.length();
String result=””;
for(int i=0;i<n;i++){
result=result+str.substring (k-n);
}
return result;
}
Problem-10
Given a string and an int n, return a string made of the first n characters of the string, followed by the first n-1 characters of the string, and so on. You may assume that n is between 0 and the length of the string, inclusive (i.e. n >= 0 and n <= str.length()).
Example
repeatFront(“Chocolate”, 4) → “ChocChoChC”
repeatFront(“Chocolate”, 3) → “ChoChC”
repeatFront(“Ice Cream”, 2) → “IcI”
Solution
public String repeatFront(String str, int n) {
String result=””;
for (int i=n;i>=0;i–){
result=result+str.substring(0,i);
}
return result;
}
Problem-11
Given two strings, word and a separator sep, return a big string made of count occurrences of the word, separated by the separator string.
Example
repeatSeparator(“Word”, “X”, 3) → “WordXWordXWord”
repeatSeparator(“This”, “And”, 2) → “ThisAndThis”
repeatSeparator(“This”, “And”, 1) → “This”
Solution
public String repeatSeparator(String word, String sep, int count) {
String result=””;
String sub=””;
for (int i=0;i<count*2-1;i++){
if (i%2==0) sub=word;
if(i%2==1) sub=sep;
result=result+sub;
}
return result;
}

Problem-12
Given a string, consider the prefix string made of the first N chars of the string. Does that prefix string appear somewhere else in the string? Assume that the string is not empty and that N is in the range 1..str.length().
Example
prefixAgain(“abXYabc”, 1) → true
prefixAgain(“abXYabc”, 2) → true
prefixAgain(“abXYabc”, 3) → false
Solution
public boolean prefixAgain(String str, int n) {
String result=str.substring(0,n);
for (int i=n;i<str.length();i++){
if(i+n>str.length()) continue;
if(result.equals(str.substring(i,i+n))) return true;
}
return false;
}
Problem-13
A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of “bread” in the given string, or return the empty string “” if there are not two pieces of bread.
Example
getSandwich(“breadjambread”) → “jam”
getSandwich(“xxbreadjambreadyy”) → “jam”
getSandwich(“xxbreadyy”) → “”
Solution
public String getSandwich(String str) {
String result=””;
int f=str.indexOf(“bread”);
int l=str.lastIndexOf(“bread”);
if(str.length()<10) result=result;
if (str.length()>10 && f!=-1 && l!=-1) result=str.substring(f+5,l);
return result;
}
Problem-14
Return true if the given string contains an appearance of “xyz” where the xyz is not directly preceeded by a period (.). So “xxyz” counts but “x.xyz” does not.
Example
xyzThere(“abcxyz”) → true
xyzThere(“abc.xyz”) → false
xyzThere(“xyz.abc”) → true
Solution
public boolean xyzThere(String str) {
for (int i=0;i<str.length();i++){
if(i+3>str.length()) continue;
if(i==0){
if(str.substring(i,i+3).equals(“xyz”)) return true;
}
if(i-1>0){
if(str.substring(i,i+3).equals(“xyz”) && !str.substring(i-1,i+3).equals(“.xyz”)) return true;
}
}
return false;
}
Problem-15
Given a string, does “xyz” appear in the middle of the string? To define middle, we’ll say that the number of chars to the left and right of the “xyz” must differ by at most one. This problem is harder than it looks.
Example
xyzMiddle(“AAxyzBB”) → true
xyzMiddle(“AxyzBB”) → true
xyzMiddle(“AxyzBBB”) → false
Solution
public boolean xyzMiddle(String str) {
if (str.length()<3) return false;
if(str.length()==3 && str.equals(“xyz”)) return true;
int pos=str.length()/2;
if (str.length()%2==0){
if (str.substring(pos-1,pos+2).equals(“xyz”) || str.substring(pos-2,pos+1).equals(“xyz”)) return true;
}
if (str.length()%2==1){
if (str.substring(pos-1,pos+2).equals(“xyz”)) return true;
}
return false;
}
Problem-16
Given a string, compute a new string by moving the first char to come after the next two chars, so “abc” yields “bca”. Repeat this process for each subsequent group of 3 chars, so “abcdef” yields “bcaefd”. Ignore any group of fewer than 3 chars at the end.
Example
oneTwo(“abc”) → “bca”
oneTwo(“tca”) → “cat”
oneTwo(“tcagdo”) → “catdog”
Solution
public String oneTwo(String str) {
String result=””;
if(str.length()<3) result=result;
for (int i=0;i<str.length();i++){
if(i+3>str.length()) break;
if(i%3==0)
result+=str.substring(i+1,i+3)+str.substring(i,i+1);
}
return result;
}

Problem-17