Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.
public String altPairs(String str) {
for(int i=0;i<str.length();i++)
String str2=(str.substring(i,i+1)+str.substring(i+4,i+5));
return str2;
}
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
public String altPairs(String str) {
int len = str.length() ;
StringBuilder result = new StringBuilder() ;
for (int i = 0 ; i < len ; i += 4) {
int end = Math.min(i+2, len) ;
String str2=(str.substring(i,i+2));
result.append(str2) ;
}
return result.toString() ;
}
public String altPairs(String str) {
String str2="";
for(int i=0;i<str.length();i++)
str2=(str.substring(i,i+1)+str.substring(i+4,i+5));
return str2;
}
However, you also need to check for the string length to make sure substring(i+4,i+5) does not go past the last character of the string.String str2="";
for(int i=0;i<str.length();i+=4)
{
str2+=str.substring(i,i+2<=str.length()?i+2:i+1);
}
return str2;
You have defined the str2 variable inside the for() scope. It needs to be defined outside of the scope:
i got it.str2+=str.substring(i,i+2<=str.length()?i+2:i+1);
what is the meaning of above codeYou need to go through the string in jumps of 4.
So the loop will be something like "i += 4" for the step.
Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9 ... so "kittens" yields "kien".
altPairs("kitten") â†’ "kien"
altPairs("Chocolate") â†’ "Chole"
altPairs("CodingHorror") â†’ "Congrr"
String str2="";
for(int i=0;i<str.length();i+=4)
{
str2+=str.substring(i,i+2<=str.length()?i+2:i+1);
}
return str2;
i,i+2<=str.length()?i+2:i+please advise1
// str2+=str.substring(i,i+2<=str.length()?i+2:i+1); is equivalent to
if(i+2<=str.length())
str2+=str.substring(i,i+2);
else
str2+=str.substring(i,i+1);
str2+=str.substring(i,i+1)i do not see i,i+1 in below statement;
str2+=str.substring(i,i+2<Please advise=str.lengt h()?i+2:i+ 1); is equivalent to
public String altPairs(String str) {
int len = str.length() ;
StringBuilder result = new StringBuilder() ;
for (int i = 0 ; i < len ; i += 4) {
int end = Math.min(i+2, len) ;
String str2=str.substring(i,end);
result.append(str2) ;
}
return result.toString() ;
}
0,1, 4,5, 8,9
Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9 ... so "kittens" yields "kien".
altPairs("kitten") â†’ "kien"
altPairs("Chocolate") â†’ "Chole"
altPairs("CodingHorror") â†’ "Congrr"
I wonder why we are incrementing by 4
The sequence is just the pair (0,1) and then adding 4 to generate the next pair (4,5) and 4 to get the next pair (8,9) etc.
for (int i = 0 ; i < len ; i += 4) {
int end = Math.min(i+2, len) ;
String str2=str.substring(i,end);
unless Math.min(i+2, len) < 2this never happens right even if you take i as 0
public String altPairs(String str) {
int len = str.length() ;
StringBuilder result = new StringBuilder() ;
for (int i = 0 ; i < len ; i += 4) {
// int end = Math.min(i+2, len) ;
int end=len;
String str2=str.substring(i,end);
result.append(str2) ;
}
return result.toString() ;
}
when i commented above Math.min function line i am failing in all test cases where length is more than 2Expected Run
altPairs("kitten") â†’ "kien" "kittenen" X
altPairs("Chocolate") â†’ "Chole" "Chocolateolatee" X
altPairs("CodingHorror") â†’ "Congrr" "CodingHorrorngHorrorrror" X
altPairs("yak") â†’ "ya" "yak" X
altPairs("ya") â†’ "ya" "ya" OK
altPairs("y") â†’ "y" "y" OK
altPairs("") â†’ "" "" OK
altPairs("ThisThatTheOther") â†’ "ThThThth" "ThisThatTheOtherThatTheOtherTheOtherther" X
0 length string("") take 2 as end..so appends 0,1 index characters which are"" and ""
1 length string("y") take 2 as end..so appends 0,1 index characters which are"y" and ""
2 length string("ya" take 2 as end..so appends 0,1 index characters which are"y" and "a"
3 length string("yak") take 2 as end..so appends 0,1 index characters which are"y" and "a" in first iteration(when i=0) and appends k in second iteration(when i=1)
6 length string("kitten") 2 as end....so appends 0,1 index characters which are"k" and "i" in first iteration(when i=0) and appends t,t in second iteration(when i=1) and appends e,n in second iteration(when i=2)
3 length string("yak") take 2 as end..so appends 0,1 index characters which are"y" and "a" in first iteration(when i=0) and appends k in second iteration(when i=1)No. After i += 4,
6 length string("kitten") 2 as end....so appends 0,1 index characters which are"k" and "i" in first iteration(when i=0) and appends t,t in second iteration(when i=1) and appends e,n in second iteration(when i=2)No. in second iteration(when i=4) "e" and "n" are appended
public String altPairs(String str) {
int len = str.length() ;
StringBuilder result = new StringBuilder() ;
for (int i = 0 ; i < len-1 ; i += 4) {
int end = Math.min(i+2, len) ;
//int end=len;
String str2=str.substring(i,end);
result.append(str2) ;
}
return result.toString() ;
}
Expected Run
altPairs("kitten") â†’ "kien" "kien" OK
altPairs("Chocolate") â†’ "Chole" "Chol" X
altPairs("CodingHorror") â†’ "Congrr" "Congrr" OK
altPairs("yak") â†’ "ya" "ya" OK
altPairs("ya") â†’ "ya" "ya" OK
altPairs("y") â†’ "y" "" X
altPairs("") â†’ "" "" OK
altPairs("ThisThatTheOther") â†’ "ThThThth" "ThThThth" OK
i wonder why they failed esp.int end = Math.min(i+2, len) ;
we are getting end to feed to substring methodcorrect
altPairs("") â†’ "" "" OKwill not go to if loop of above solution right.
altPairs("") â†’ "" "" OK
public String altPairs(String str) {
int len = str.length() ;
StringBuilder result = new StringBuilder() ;
int i;
for ( i = 0 ; i < len-1 ; i += 4) {
result.append(str.substring(i,i+2)) ;
}
if( i==len-1 ){
result.append(str.substring(i)) ;
}
return result.toString() ;
}
for ( i = 0 ; i < len-1 ; i += 4) does not go into the loop body when len==0 or len==1
if len==0, the solution of http#a40454432 reduces toi could not see above mentioned url--a40454432
if len==0, the solution of http#a40454432 reduces to
public String altPairs(String str) {
int len = str.length() ;
StringBuilder result = new StringBuilder() ;
int i;
// don't execute code in for loop body, don't execute code in if body
return result.toString() ;
}
should not be as below? please advisepublic String altPairs(String str) {
int len = str.length() ;
StringBuilder result = new StringBuilder() ;
int i;
for ( i = 0 ; i < len-1 ; i += 4) {
result.append(str.substring(i,i+2)) ;
}
if( i==len-1 ){
result.append(str.substring(i)) ;
}
return result.toString() ;
}
if the len==0 then it directly goes toThat's correct
return result.toString() ;
// str2+=str.substring(i,i+2<=str.length()?i+2:i+1); is equivalent to
if(i+2<=str.length())
str2+=str.substring(i,i+2);
else
str2+=str.substring(i,i+1)
altPairs("kitten") â†’ "kien" "kien" OK
for above example str.length is 6 rightstr2+=str.substring(i,i+2<=str.length()?i+2:i+1);
i+2<=str.length()?i+2:i+1 becomes 2<=6 since true takes i+2 which is 2 _ _
k i t t e n
| ^ | ^ ^ ^ ^
0 1 2 3 4 5 6
"kitten".substring(0,2) is "ki", which we want
Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9 ... so "kittens" yields "kien".
altPairs("kitten") â†’ "kien"
altPairs("Chocolate") â†’ "Chole"
altPairs("CodingHorror") â†’ "Congrr"
altPairs("y") â†’ "y" "y" OK
altPairs("") â†’ "" "" OK
i+2<=str.length()?i+2:i+1
i+2<=please advise
altPairs("CodingHorror") â†’ "Congrr" "Congrr" OK
for(int i=0;i<str.length();i+=4)the loop condition starts out false, so we don't even enter the loop body when i=8 right?
public class Test33 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//altPairs("chocolate");
System.out.println("value is-->"+altPairs("chocolate"));
}
public static String altPairs(String str) {
String str2="";
for(int i=0;i<str.length();i+=4)
{
str2+=str.substring(i,i+2<=str.length()?i+2:i+1);
}
return str2;
}
}
i got outputi got output
value is-->chole
how we got chole??
substring second argument is
i+2<=str.length()?i+2:i+1
which means
if i+2<=str.length() is true take i+2 (since2<=8)
so we get substring(0,2) which is "ch" in the first iteration
then i incremented by 4 so 0 becomes 4
6<=8 true so take i+2
so we get substring(4,6) which is "ol" in the second iteration
//chocolate
then i incremented by 4 so 4 becomes 8
10<=8 false so take i+1
so we get substring(8,9) which is "e" in the third iteration
so i+1 is coming right.
i got outputmy above understanding is correct right? please advise
value is-->chole
how we got chole??
substring second argument is
i+2<=str.length()?i+2:i+1
which means
if i+2<=str.length() is true take i+2 (since2<=9)
so we get substring(0,2) which is "ch" in the first iteration
then i incremented by 4 so 0 becomes 4
6<=9 true so take i+2
so we get substring(4,6) which is "ol" in the second iteration
//chocolate
then i incremented by 4 so 4 becomes 8
10<=9 false so take i+1
so we get substring(8,9) which is "e" in the third iteration
so i+1 is coming right.
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.