## Problem:

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. This is a little harder than it looks.

withoutX2("xHi") → "Hi"
withoutX2("Hxi") → "Hi"
withoutX2("Hi") → "Hi"

## Solution:

public String withoutX2(String str) {
String temp = "";

for (int i = 0; i < str.length(); i++) {
if (i == 0 && str.charAt(i) != 'x')
temp += str.charAt(i);
else if (i == 1 && str.charAt(i) != 'x')
temp += str.charAt(i);
else if (i > 1)
temp += str.charAt(i);
}

return temp;
}

1. OMG another solution with a loop when is not supposed to? GG!

1. public String withoutX2(String str) {

if(str.length()==0){
return str;
}

if(str.length()==1){
if(str.equals("x")){
return "";
}
else{
return str;
}

}

return str.substring(0,2).replaceAll("x","")+str.substring(2);

}

2. public String withoutX2(String str) {
String r=str;
if (str.length()>1 && str.charAt(1)=='x')
r=str.substring(0,1)+str.substring(2);
if (str.length()>0 && str.charAt(0)=='x')
r=r.substring(1);
return r;
}

3. public String withoutX2(String str){
if(str.length() > 1 && str.charAt(1) == 'x')
str = str.substring(0, 1) + str.substring(2);
if(str.length() > 0 && str.charAt(0) == 'x')
str = str.substring(1);
return str;
}

1. This is likely the solution the designer of the exercises wished to see. No loops, no use of StringBuilder, one return statement.

2. public String without2(String str) {
if(str.length() > 1 && str.charAt(1) == 'x')
str = str.substring(0, 1) + str.substring(2);
if(str.length() > 0 && str.charAt(0) == 'x')
str = str.substring(1);
return str;
}

4. Here is a shorter solution:

public String withoutX2(String str)
{
String word = "";
for(int i=0; i<str.length(); i++)
{
if((i==0||i==1) && str.charAt(i)=='x')
continue;
word += str.charAt(i);
}
return word;
}

5. // if want to know how not to solve it correctly

public String withoutX2(String str) {

int i=0;
char ch ;
String temp ="";

for( i=0;i<str.length();i++){
ch = str.charAt(i);
if( ch == 'x'){
if(i<2){
ch ='_';
}
}
temp += ch;
}

return temp.replace("_","");

}

6. public String withoutX2(String str) {
StringBuilder str2 = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'x' && i < 2)
continue;
str2.append(str.charAt(i));
}
return str2.toString();
}

7. public String withoutX2(String str) {
if(str.length() == 0) {
return "";
}
if(str.length() == 1 && str.charAt(0) == 'x'){
return "";
}
String twoFirstLetter = str.substring(0, 2);

int indexFirstX = twoFirstLetter.charAt(0) == 'x' ? 0 : -1;
int indexSecondX = twoFirstLetter.charAt(1) == 'x' ? 1 : -1;

if(indexFirstX != -1 && indexSecondX != -1){
return str.substring(2);
}
if(indexFirstX != -1 && indexSecondX == -1){
return str.substring(1);
}
if(indexFirstX == -1 && indexSecondX == 1){
return twoFirstLetter.charAt(0) + str.substring(2);
}
return str;
}

8. public String withoutX2(String str) {
String result = "";
if (str.equals("x") || str.equals("xx") || str.equals(result)) {
return result;
}
if (str.length() > 1) {
String tmp = str.substring(0, 2);
tmp = tmp.replaceAll("x", "");
result = tmp + str.substring(2);
}
return result;
}

9. int count = 0;
for (int i = 0; i < str.length()-1; i++ ){
if (str.substring(i,i+2).equals("hi"))
count++;
}
return count;

10. int count = 0;
for (int i = 0; i < str.length()-1; i++ ){
if (str.substring(i,i+2).equals("hi"))
count++;
}
return count;

11. int count = 0;
for (int i = 0; i < str.length()-1; i++ ){
if (str.substring(i,i+2).equals("hi"))
count++;
}
return count;

12. public String withoutX2(String str) {
String withoutX= "";
if (str.length() >2)
{
String firstX = str.substring(0,1);
String secondX = str.substring(1,2);
String lastX = str.substring(2);
if(!firstX.equals("x"))
{
withoutX+=firstX;
}
if(!secondX.equals("x"))
{
withoutX+=secondX;
}

withoutX+=lastX;
}
else if (str.length()==1)
{
if (!str.substring(0,1).equals("x"))
{
withoutX+=str.substring(0,1);
}
}
else if (str.length()==2)
{
if (!str.substring(0,1).equals("x"))
{
withoutX+=str.substring(0,1);
}
if (!str.substring(1,2).equals("x"))
{
withoutX+=str.substring(1);
}
}

return withoutX;
}

13. if(str.equals("x") || str.isEmpty()){
return "";
}
String firstTwo = str.substring(0,2);
String replacement = firstTwo.replaceAll("x","");

if(firstTwo.contains("x")){
return replacement + str.substring(2);

}
return str;

14. public String withoutX2(String str) {
if(str.equals("x") || str.isEmpty()){
return "";
}
if(str.substring(0, 2).contains("x")){
return str.substring(0, 2).replaceAll("x","") + str.substring(2);
}
return str;
}

15. public String withoutX2(String str) {
if(str.equals("x") || str.isEmpty()){
return "";
}
if(str.substring(0, 2).contains("x")){
return str.substring(0, 2).replaceAll("x","") + str.substring(2);
}
return str;
}

16. public String withoutX2(String str) {
if(str.equals("x") || str.isEmpty()){
return "";
}
if(str.substring(0, 2).contains("x")){
return str.substring(0, 2).replaceAll("x","") + str.substring(2);
}
return str;
}

17. public String withoutX2(String str) {
if (str.length() <= 1) return "";
String temp = str.substring(0, 2);
str = str.substring(2);
temp = temp.replace("x", "");
return temp.concat(str);
}

18. public String withoutX2(String str) {
if(str.length()==0)
return "";
String rtr = "";
if(str.charAt(0)!='x')
rtr+=str.charAt(0);
if(str.length()>1 && str.charAt(1)!='x')
rtr+=str.charAt(1);
if(str.length()>2)
rtr = rtr + str.substring(2);

return rtr;
}

19. Another way

public String withoutX2(String str) {
if (str.isEmpty()) return "";
if (str.startsWith("xx")) return str.substring(2);
if (str.startsWith("x")) return str.substring(1);
if (str.substring(1, 2).equals("x")) return str.substring(0, 1) + str.substring(2);
return str;
}