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; }
OMG another solution with a loop when is not supposed to? GG!
ReplyDeletepublic String withoutX2(String str) {
Deleteif(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);
}
public String withoutX2(String str) {
ReplyDeleteString 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;
}
+1
DeleteTHANX bro
Deletepublic String withoutX2(String str){
ReplyDeleteif(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;
}
This is likely the solution the designer of the exercises wished to see. No loops, no use of StringBuilder, one return statement.
Deletepublic String without2(String str) {
Deleteif(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;
}
Here is a shorter solution:
ReplyDeletepublic 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;
}
// if want to know how not to solve it correctly
ReplyDeletepublic 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("_","");
}
public String withoutX2(String str) {
ReplyDeleteStringBuilder 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();
}
public String withoutX2(String str) {
ReplyDeleteif(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;
}
public String withoutX2(String str) {
ReplyDeleteString 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;
}
int count = 0;
ReplyDeletefor (int i = 0; i < str.length()-1; i++ ){
if (str.substring(i,i+2).equals("hi"))
count++;
}
return count;
int count = 0;
ReplyDeletefor (int i = 0; i < str.length()-1; i++ ){
if (str.substring(i,i+2).equals("hi"))
count++;
}
return count;
int count = 0;
ReplyDeletefor (int i = 0; i < str.length()-1; i++ ){
if (str.substring(i,i+2).equals("hi"))
count++;
}
return count;
public String withoutX2(String str) {
ReplyDeleteString 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;
}
if(str.equals("x") || str.isEmpty()){
ReplyDeletereturn "";
}
String firstTwo = str.substring(0,2);
String replacement = firstTwo.replaceAll("x","");
if(firstTwo.contains("x")){
return replacement + str.substring(2);
}
return str;
public String withoutX2(String str) {
ReplyDeleteif(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;
}
public String withoutX2(String str) {
ReplyDeleteif(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;
}
public String withoutX2(String str) {
ReplyDeleteif(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;
}
public String withoutX2(String str) {
ReplyDeleteif (str.length() <= 1) return "";
String temp = str.substring(0, 2);
str = str.substring(2);
temp = temp.replace("x", "");
return temp.concat(str);
}
public String withoutX2(String str) {
ReplyDeleteif(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;
}
Another way
ReplyDeletepublic 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;
}