gudii9
asked on
oneTwo java challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p122943
I tried my code as below
cannot i give increment as i+2 instead of i++ in for loop?
how to improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
Please advise
I am working on below challenge
http://codingbat.com/prob/p122943
I tried my code as below
public String oneTwo(String str) {
StringBuilder sb=new StringBuilder();
int strLen=str.length();
for(int i=0;i<=strLen-3;i+2){
sb.append(str.substring(i,i+3));
}
return sb.toString();
}
I am getting below resultCompile problems:
Error: for(int i=0;i<=strLen-3;i+2){
^
Syntax error on token "+", invalid AssignmentOperator
see Example Code to help with compile problems
cannot i give increment as i+2 instead of i++ in for loop?
how to improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
Please advise
ASKER
public String oneTwo(String str) {
StringBuilder sb=new StringBuilder();
int strLen=str.length();
for(int i=0;i<=strLen-3;i++){
sb.append(str.substring(i,i+3));
//sb.append(
i=i+2;
}
return sb.toString();
}
gives below results
Expected Run
oneTwo("abc") → "bca" "abc" X
oneTwo("tca") → "cat" "tca" X
oneTwo("tcagdo") → "catdog" "tcagdo" X
oneTwo("chocolate") → "hocolctea" "chocolate" X
oneTwo("1234567890") → "231564897" "123456789" X
oneTwo("xabxabxabxabxabxabxab") → "abxabxabxabxabxabxabx" "xabxabxabxabxabxabxab" X
oneTwo("abcdefx") → "bcaefd" "abcdef" X
oneTwo("abcdefxy") → "bcaefd" "abcdef" X
oneTwo("abcdefxyz") → "bcaefdyzx" "abcdefxyz" X
oneTwo("") → "" "" OK
oneTwo("x") → "" "" OK
oneTwo("xy") → "" "" OK
oneTwo("xyz") → "yzx" "xyz" X
oneTwo("abcdefghijklkmnopqrstuvwxyz1 234567890" ) → "bcaefdhigkljmnkpqostrvwuy zx23156489 7" "abcdefghijklkmnopqrstuvwx yz12345678 9" X
oneTwo("abcdefghijklkmnopqrstuvwxyz1 23456789") → "bcaefdhigkljmnkpqostrvwuy zx23156489 7" "abcdefghijklkmnopqrstuvwx yz12345678 9" X
oneTwo("abcdefghijklkmnopqrstuvwxyz1 2345678") → "bcaefdhigkljmnkpqostrvwuy zx231564" "abcdefghijklkmnopqrstuvwx yz123456" X
other tests
Please forgive the mishandling of the letter i. It's a darn Micorsoft style "feature" that capitalizes that letter when it's by itself. Wherever you see a capital i ("I") it should be lower case.
ASKER
public String oneTwo(String str) {
StringBuilder sb=new StringBuilder();
StringBuilder sb1=new StringBuilder();
StringBuilder sb2=new StringBuilder();
StringBuilder sb3=new StringBuilder();
int strLen=str.length();
for(int i=0;i<=strLen-3;i+=3){
// int i = i+=n.
//sb.append(str.substring(i,i+3));
sb1.append(str.substring(i));
sb2.append(str.substring(i+2,i+3));
sb3=sb2.append(sb1);
//sb.append(str.substring(i,i+1))
//sb.append(
//i=i+2;
}
return sb3.toString();
}
above give below results
Expected Run
oneTwo("abc") → "bca" "cabc" X
oneTwo("tca") → "cat" "atca" X
oneTwo("tcagdo") → "catdog" "atcagdootcagdogdo" X
oneTwo("chocolate") → "hocolctea" "ochocolatelchocolatecolateechocolat ecolateate " X
oneTwo("1234567890") → "231564897" "31234567890612345678904567890912345 6789045678 907890" X
oneTwo("xabxabxabxabxabxabxab") → "abxabxabxabxabxabxabx" "bxabxabxabxabxabxabxabbxa bxabxabxab xabxabxabx abxabxabxa bxabxabbxa bxabxabxab xabxabxabx abxabxabxa bxabxabxab xabxabxabx abbxabxabx abxabxabxa bxabxabxab xabxabxabx abxabxabxa bxabxabxab xabxabxabb xabxabxabx abxabxabxa bxabxabxab xabxabxabx abxabxabxa bxabxabxab xabxabxabx abxabbxabx abxabxabxa bxabxabxab xabxabxabx abxabxabxa bxabxabxab xabxabxabx abxabxabxa bxabxabbxa bxabxabxab xabxabxabx abxabxabxa bxabxabxab xabxabxabx abxabxabxa bxabxabxab xabxabxabx ab" X
oneTwo("abcdefx") → "bcaefd" "cabcdefxfabcdefxdefx" X
oneTwo("abcdefxy") → "bcaefd" "cabcdefxyfabcdefxydefxy" X
oneTwo("abcdefxyz") → "bcaefdyzx" "cabcdefxyzfabcdefxyzdefxyzzabcdefxy zdefxyzxyz " X
oneTwo("") → "" "" OK
oneTwo("x") → "" "" OK
oneTwo("xy") → "" "" OK
oneTwo("xyz") → "yzx" "zxyz" X
oneTwo("abcdefghijklkmnopqrstuvwxyz1 234567890" ) → "bcaefdhigkljmnkpqostrvwuy zx23156489 7" "cabcdefghijklkmnopqrstuvw xyz1234567 890fabcdef ghijklkmno pqrstuvwxy z123456789 0defghijkl kmnopqrstu vwxyz12345 67890iabcd efghijklkm nopqrstuvw xyz1234567 890defghij klkmnopqrs tuvwxyz123 4567890ghi jklkmnopqr stuvwxyz12 34567890la bcdefghijk lkmnopqrst uvwxyz1234 567890defg hijklkmnop qrstuvwxyz 1234567890 ghijklkmno pqrstuvwxy z123456789 0jklkmnopq rstuvwxyz1 234567890n abcdefghij klkmnopqrs tuvwxyz123 4567890def ghijklkmno pqrstuvwxy z123456789 0ghijklkmn opqrstuvwx yz12345678 90jklkmnop qrstuvwxyz 1234567890 kmnopqrstu vwxyz12345 67890qabcd efghijklkm nopqrstuvw xyz1234567 890defghij klkmnopqrs tuvwxyz123 4567890ghi jklkmnopqr stuvwxyz12 34567890jk lkmnopqrst uvwxyz1234 567890kmno pqrstuvwxy z123456789 0opqrstuvw xyz1234567 890tabcdef ghijklkmno pqrstuvwxy z123456789 0defghijkl kmnopqrstu vwxyz12345 67890ghijk lkmnopqrst uvwxyz1234 567890jklk mnopqrstuv wxyz123456 7890kmnopq rstuvwxyz1 234567890o pqrstuvwxy z123456789 0rstuvwxyz 1234567890 wabcdefghi jklkmnopqr stuvwxyz12 34567890de fghijklkmn opqrstuvwx yz12345678 90ghijklkm nopqrstuvw xyz1234567 890jklkmno pqrstuvwxy z123456789 0kmnopqrst uvwxyz1234 567890opqr stuvwxyz12 34567890rs tuvwxyz123 4567890uvw xyz1234567 890zabcdef ghijklkmno pqrstuvwxy z123456789 0defghijkl kmnopqrstu vwxyz12345 67890ghijk lkmnopqrst uvwxyz1234 567890jklk mnopqrstuv wxyz123456 7890kmnopq rstuvwxyz1 234567890o pqrstuvwxy z123456789 0rstuvwxyz 1234567890 uvwxyz1234 567890xyz1 2345678903 abcdefghij klkmnopqrs tuvwxyz123 4567890def ghijklkmno pqrstuvwxy z123456789 0ghijklkmn opqrstuvwx yz12345678 90jklkmnop qrstuvwxyz 1234567890 kmnopqrstu vwxyz12345 67890opqrs tuvwxyz123 4567890rst uvwxyz1234 567890uvwx yz12345678 90xyz12345 6789012345 678906abcd efghijklkm nopqrstuvw xyz1234567 890defghij klkmnopqrs tuvwxyz123 4567890ghi jklkmnopqr stuvwxyz12 34567890jk lkmnopqrst uvwxyz1234 567890kmno pqrstuvwxy z123456789 0opqrstuvw xyz1234567 890rstuvwx yz12345678 90uvwxyz12 34567890xy z123456789 0123456789 045678909a bcdefghijk lkmnopqrst uvwxyz1234 567890defg hijklkmnop qrstuvwxyz 1234567890 ghijklkmno pqrstuvwxy z123456789 0jklkmnopq rstuvwxyz1 234567890k mnopqrstuv wxyz123456 7890opqrst uvwxyz1234 567890rstu vwxyz12345 67890uvwxy z123456789 0xyz123456 7890123456 7890456789 07890" X
oneTwo("abcdefghijklkmnopqrstuvwxyz1 23456789") → "bcaefdhigkljmnkpqostrvwuy zx23156489 7" "cabcdefghijklkmnopqrstuvw xyz1234567 89fabcdefg hijklkmnop qrstuvwxyz 123456789d efghijklkm nopqrstuvw xyz1234567 89iabcdefg hijklkmnop qrstuvwxyz 123456789d efghijklkm nopqrstuvw xyz1234567 89ghijklkm nopqrstuvw xyz1234567 89labcdefg hijklkmnop qrstuvwxyz 123456789d efghijklkm nopqrstuvw xyz1234567 89ghijklkm nopqrstuvw xyz1234567 89jklkmnop qrstuvwxyz 123456789n abcdefghij klkmnopqrs tuvwxyz123 456789defg hijklkmnop qrstuvwxyz 123456789g hijklkmnop qrstuvwxyz 123456789j klkmnopqrs tuvwxyz123 456789kmno pqrstuvwxy z123456789 qabcdefghi jklkmnopqr stuvwxyz12 3456789def ghijklkmno pqrstuvwxy z123456789 ghijklkmno pqrstuvwxy z123456789 jklkmnopqr stuvwxyz12 3456789kmn opqrstuvwx yz12345678 9opqrstuvw xyz1234567 89tabcdefg hijklkmnop qrstuvwxyz 123456789d efghijklkm nopqrstuvw xyz1234567 89ghijklkm nopqrstuvw xyz1234567 89jklkmnop qrstuvwxyz 123456789k mnopqrstuv wxyz123456 789opqrstu vwxyz12345 6789rstuvw xyz1234567 89wabcdefg hijklkmnop qrstuvwxyz 123456789d efghijklkm nopqrstuvw xyz1234567 89ghijklkm nopqrstuvw xyz1234567 89jklkmnop qrstuvwxyz 123456789k mnopqrstuv wxyz123456 789opqrstu vwxyz12345 6789rstuvw xyz1234567 89uvwxyz12 3456789zab cdefghijkl kmnopqrstu vwxyz12345 6789defghi jklkmnopqr stuvwxyz12 3456789ghi jklkmnopqr stuvwxyz12 3456789jkl kmnopqrstu vwxyz12345 6789kmnopq rstuvwxyz1 23456789op qrstuvwxyz 123456789r stuvwxyz12 3456789uvw xyz1234567 89xyz12345 67893abcde fghijklkmn opqrstuvwx yz12345678 9defghijkl kmnopqrstu vwxyz12345 6789ghijkl kmnopqrstu vwxyz12345 6789jklkmn opqrstuvwx yz12345678 9kmnopqrst uvwxyz1234 56789opqrs tuvwxyz123 456789rstu vwxyz12345 6789uvwxyz 123456789x yz12345678 9123456789 6abcdefghi jklkmnopqr stuvwxyz12 3456789def ghijklkmno pqrstuvwxy z123456789 ghijklkmno pqrstuvwxy z123456789 jklkmnopqr stuvwxyz12 3456789kmn opqrstuvwx yz12345678 9opqrstuvw xyz1234567 89rstuvwxy z123456789 uvwxyz1234 56789xyz12 3456789123 4567894567 899abcdefg hijklkmnop qrstuvwxyz 123456789d efghijklkm nopqrstuvw xyz1234567 89ghijklkm nopqrstuvw xyz1234567 89jklkmnop qrstuvwxyz 123456789k mnopqrstuv wxyz123456 789opqrstu vwxyz12345 6789rstuvw xyz1234567 89uvwxyz12 3456789xyz 1234567891 2345678945 6789789" X
oneTwo("abcdefghijklkmnopqrstuvwxyz1 2345678") → "bcaefdhigkljmnkpqostrvwuy zx231564" "cabcdefghijklkmnopqrstuvw xyz1234567 8fabcdefgh ijklkmnopq rstuvwxyz1 2345678def ghijklkmno pqrstuvwxy z12345678i abcdefghij klkmnopqrs tuvwxyz123 45678defgh ijklkmnopq rstuvwxyz1 2345678ghi jklkmnopqr stuvwxyz12 345678labc defghijklk mnopqrstuv wxyz123456 78defghijk lkmnopqrst uvwxyz1234 5678ghijkl kmnopqrstu vwxyz12345 678jklkmno pqrstuvwxy z12345678n abcdefghij klkmnopqrs tuvwxyz123 45678defgh ijklkmnopq rstuvwxyz1 2345678ghi jklkmnopqr stuvwxyz12 345678jklk mnopqrstuv wxyz123456 78kmnopqrs tuvwxyz123 45678qabcd efghijklkm nopqrstuvw xyz1234567 8defghijkl kmnopqrstu vwxyz12345 678ghijklk mnopqrstuv wxyz123456 78jklkmnop qrstuvwxyz 12345678km nopqrstuvw xyz1234567 8opqrstuvw xyz1234567 8tabcdefgh ijklkmnopq rstuvwxyz1 2345678def ghijklkmno pqrstuvwxy z12345678g hijklkmnop qrstuvwxyz 12345678jk lkmnopqrst uvwxyz1234 5678kmnopq rstuvwxyz1 2345678opq rstuvwxyz1 2345678rst uvwxyz1234 5678wabcde fghijklkmn opqrstuvwx yz12345678 defghijklk mnopqrstuv wxyz123456 78ghijklkm nopqrstuvw xyz1234567 8jklkmnopq rstuvwxyz1 2345678kmn opqrstuvwx yz12345678 opqrstuvwx yz12345678 rstuvwxyz1 2345678uvw xyz1234567 8zabcdefgh ijklkmnopq rstuvwxyz1 2345678def ghijklkmno pqrstuvwxy z12345678g hijklkmnop qrstuvwxyz 12345678jk lkmnopqrst uvwxyz1234 5678kmnopq rstuvwxyz1 2345678opq rstuvwxyz1 2345678rst uvwxyz1234 5678uvwxyz 12345678xy z123456783 abcdefghij klkmnopqrs tuvwxyz123 45678defgh ijklkmnopq rstuvwxyz1 2345678ghi jklkmnopqr stuvwxyz12 345678jklk mnopqrstuv wxyz123456 78kmnopqrs tuvwxyz123 45678opqrs tuvwxyz123 45678rstuv wxyz123456 78uvwxyz12 345678xyz1 2345678123 456786abcd efghijklkm nopqrstuvw xyz1234567 8defghijkl kmnopqrstu vwxyz12345 678ghijklk mnopqrstuv wxyz123456 78jklkmnop qrstuvwxyz 12345678km nopqrstuvw xyz1234567 8opqrstuvw xyz1234567 8rstuvwxyz 12345678uv wxyz123456 78xyz12345 6781234567 845678" X
other tests
You missed the two main points I showed earlier. i = i+2 is not the way to increment plus you want to increment by 3, so you should use i+=3; As long as i is less than or equal to the length of the string minus three, it will produce indexes of 0,3,6,9,12, etc. which is where you need to find each"trio" of letters in the string. Secondly, appending str.substring(i, i +3) would simply produce the three letter trio, so you need to append the second two characters followed by appending the first character.
"abc".substring(0,3) = "abc" ==> not what you want
"abc" substring (1,3) = "bc" ==> append this
"abc" substring (1,2) = "a" ==> then this
or concatenate them, "bc" + "a" and append the concatenation.
"abc".substring(0,3) = "abc" ==> not what you want
"abc" substring (1,3) = "bc" ==> append this
"abc" substring (1,2) = "a" ==> then this
or concatenate them, "bc" + "a" and append the concatenation.
ASKER
public String oneTwo(String str) {
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();
int strLen = str.length();
for (int i = 0; i <= strLen - 3; i += 3) {
// int i = i+=n.
//sb.append(str.substring(i,i+3));
sb1.append(str.substring(i, i + 1));
sb2.append(str.substring(i + 1, i + 3));
sb3 = sb2.append(sb1);
//sb.append(str.substring(i,i+1))
//sb.append(
//i=i+2;
}
return sb3.toString();
}
above passed few more
Expected Run
oneTwo("abc") → "bca" "bca" OK
oneTwo("tca") → "cat" "cat" OK
oneTwo("tcagdo") → "catdog" "catdotg" X
oneTwo("chocolate") → "hocolctea" "hocolcctecca" X
oneTwo("1234567890") → "231564897" "231561489147" X
oneTwo("xabxabxabxabxabxabxab") → "abxabxabxabxabxabxabx" "abxabxxabxxxabxxxxabxxxxx abxxxxxxab xxxxxxx" X
oneTwo("abcdefx") → "bcaefd" "bcaefad" X
oneTwo("abcdefxy") → "bcaefd" "bcaefad" X
oneTwo("abcdefxyz") → "bcaefdyzx" "bcaefadyzadx" X
oneTwo("") → "" "" OK
oneTwo("x") → "" "" OK
oneTwo("xy") → "" "" OK
oneTwo("xyz") → "yzx" "yzx" OK
oneTwo("abcdefghijklkmnopqrstuvwxyz1 234567890" ) → "bcaefdhigkljmnkpqostrvwuy zx23156489 7" "bcaefadhiadgkladgjmnadgjk pqadgjkost adgjkorvwa dgjkoruyza dgjkorux23 adgjkorux1 56adgjkoru x1489adgjk orux147" X
oneTwo("abcdefghijklkmnopqrstuvwxyz1 23456789") → "bcaefdhigkljmnkpqostrvwuy zx23156489 7" "bcaefadhiadgkladgjmnadgjk pqadgjkost adgjkorvwa dgjkoruyza dgjkorux23 adgjkorux1 56adgjkoru x1489adgjk orux147" X
oneTwo("abcdefghijklkmnopqrstuvwxyz1 2345678") → "bcaefdhigkljmnkpqostrvwuy zx231564" "bcaefadhiadgkladgjmnadgjk pqadgjkost adgjkorvwa dgjkoruyza dgjkorux23 adgjkorux1 56adgjkoru x14" X
other tests
ASKER
public String oneTwo(String str) {
StringBuilder sb=new StringBuilder();
StringBuilder sb1=new StringBuilder();
StringBuilder sb2=new StringBuilder();
StringBuilder sb3=new StringBuilder();
int strLen=str.length();
testlbl:
for(int i=0;i<=strLen-3;i+=3){
testlb2:
for(int j=0;j<=strLen-3;j+=2){
sb1.append(str.substring(j,j+1));
sb2.append(str.substring(j+1,j+3));
sb3=sb2.append(sb1);
j=j+3;
break testlb2;
}
}
return sb3.toString();
}
tried some labels but not able to fix
Expected Run
oneTwo("abc") → "bca" "bca" OK
oneTwo("tca") → "cat" "cat" OK
oneTwo("tcagdo") → "catdog" "catcatt" X
oneTwo("chocolate") → "hocolctea" "hochocchoccc" X
oneTwo("1234567890") → "231564897" "231231123111" X
oneTwo("xabxabxabxabxabxabxab") → "abxabxabxabxabxabxabx" "abxabxxabxxxabxxxxabxxxxx abxxxxxxab xxxxxxx" X
oneTwo("abcdefx") → "bcaefd" "bcabcaa" X
oneTwo("abcdefxy") → "bcaefd" "bcabcaa" X
oneTwo("abcdefxyz") → "bcaefdyzx" "bcabcaabcaaa" X
oneTwo("") → "" "" OK
oneTwo("x") → "" "" OK
oneTwo("xy") → "" "" OK
oneTwo("xyz") → "yzx" "yzx" OK
oneTwo("abcdefghijklkmnopqrstuvwxyz1 234567890" ) → "bcaefdhigkljmnkpqostrvwuy zx23156489 7" "bcabcaabcaaabcaaaabcaaaaa bcaaaaaabc aaaaaaabca aaaaaaabca aaaaaaaabc aaaaaaaaaa bcaaaaaaaa aaabcaaaaa aaaaaaa" X
oneTwo("abcdefghijklkmnopqrstuvwxyz1 23456789") → "bcaefdhigkljmnkpqostrvwuy zx23156489 7" "bcabcaabcaaabcaaaabcaaaaa bcaaaaaabc aaaaaaabca aaaaaaabca aaaaaaaabc aaaaaaaaaa bcaaaaaaaa aaabcaaaaa aaaaaaa" X
oneTwo("abcdefghijklkmnopqrstuvwxyz1 2345678") → "bcaefdhigkljmnkpqostrvwuy zx231564" "bcabcaabcaaabcaaaabcaaaaa bcaaaaaabc aaaaaaabca aaaaaaabca aaaaaaaabc aaaaaaaaaa bcaaaaaaaa aaa" X
other tests
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Go back and run those System.out.println(sb.toSt ring()) statements after creation of each StringBuilder to show what is being returned for each.
ASKER
for string length 3 and less works fine but more than 3 fails
ASKER
public class OneTwoEx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(oneTwo("tcagdo"));//catdotg shoudl come
}
public static String oneTwo(String str) {
StringBuilder sb=new StringBuilder();
StringBuilder sb1=new StringBuilder();
StringBuilder sb2=new StringBuilder();
StringBuilder sb3=new StringBuilder();
int strLen=str.length();
for(int i=0;i<=strLen-3;i+=3){
// int i = i+=n.
//sb.append(str.substring(i,i+3));
sb1.append(str.substring(i,i+1));
System.out.println("value of sb1--->"+sb1.toString());
sb2.append(str.substring(i+1,i+3));
System.out.println("value of sb2--->"+sb2.toString());
sb3=sb2.append(sb1);
System.out.println("value of sb3--->"+sb3.toString());
//sb.append(str.substring(i,i+1))
//sb.append(
//i=i+2;
}
return sb3.toString();
}
}
above gives below wrong result
value of sb1--->t
value of sb2--->ca
value of sb3--->cat
value of sb1--->tg
value of sb2--->catdo
value of sb3--->catdotg
catdotg
ASKER
public class OneTwoEx {
/**
* @param args
*/
/*
* 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.
oneTwo("abc") → "bca"
oneTwo("tca") → "cat"
oneTwo("tcagdo") → "catdog"
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(oneTwo("tcagdo"));//catdotg shoudl come
}
public static String oneTwo(String str) {
StringBuilder sb=new StringBuilder();
StringBuilder sb1=new StringBuilder();
StringBuilder sb2=new StringBuilder();
StringBuilder sb3=new StringBuilder();
int strLen=str.length();
for(int i=0;i<=strLen-3;i+=3){
// int i = i+=n.
//sb.append(str.substring(i,i+3));
sb1.append(str.substring(i,i+1));
System.out.println("value of sb1--->"+sb1.toString());
sb2.append(str.substring(i+1,i+3));
System.out.println("value of sb2--->"+sb2.toString());
sb3=sb2.append(sb1);
System.out.println("value of sb3--->"+sb3.toString());
//sb.append(str.substring(i,i+1))
//sb.append(
//i=i+2;
}
return sb3.toString();
}
}
above fixed part of it as below
value of sb1--->t
value of sb2--->ca
value of sb3--->cat
value of sb1--->tg
value of sb2--->catdo
value of sb3--->catdotg
catdotg
ASKER
value of sb3--->catdotg
not sure why t coming before g
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public class OneTwoEx {
/**
* @param args
*/
/*
* 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.
oneTwo("abc") → "bca"
oneTwo("tca") → "cat"
oneTwo("tcagdo") → "catdog"
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(oneTwo("tcagdo"));//catdotg shoudl come
}
public static String oneTwo(String str) {
StringBuilder sb=new StringBuilder();
//StringBuilder sb1=new StringBuilder();
//StringBuilder sb2=new StringBuilder();
//StringBuilder sb3=new StringBuilder();
int strLen=str.length();
for(int i=0;i<=strLen-3;i+=3){
// int i = i+=n.
//sb.append(str.substring(i,i+3));
String single=str.substring(i,i+1);
System.out.println("value of single--->"+single);
String twoChar=str.substring(i+1,i+3);
System.out.println("value of twoChar--->"+twoChar);
sb=sb.append(twoChar).append(single);
//System.out.println("value of sb3--->"+sb3.toString());
//sb.append(str.substring(i,i+1))
//sb.append(
//i=i+2;
}
return sb.toString();
}
}
above passed all tests
what is difference between below
sb=sb.append(twoChar).appe
and below
sb.append(twoChar).append(
also how many times we can chain append append append
sb=sb.append(twoChar).appe
ASKER
public String oneTwo(String str) {
return str.replaceAll("(.)(..)|.+","$2$1");
}
not clear on above.
please advise
ASKER
public String oneTwo(String str) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= str.length() - 3; i+=3) {
String next = str.substring( i + 1, i + 3);
String get1 = str.substring( i, i + 1);
sb.append(next).append(get1);
}
return sb.toString();
}
above passed all tests
Expected Run
oneTwo("abc") → "bca" "bca" OK
oneTwo("tca") → "cat" "cat" OK
oneTwo("tcagdo") → "catdog" "catdog" OK
oneTwo("chocolate") → "hocolctea" "hocolctea" OK
oneTwo("1234567890") → "231564897" "231564897" OK
oneTwo("xabxabxabxabxabxabxab") → "abxabxabxabxabxabxabx" "abxabxabxabxabxabxabx" OK
oneTwo("abcdefx") → "bcaefd" "bcaefd" OK
oneTwo("abcdefxy") → "bcaefd" "bcaefd" OK
oneTwo("abcdefxyz") → "bcaefdyzx" "bcaefdyzx" OK
oneTwo("") → "" "" OK
oneTwo("x") → "" "" OK
oneTwo("xy") → "" "" OK
oneTwo("xyz") → "yzx" "yzx" OK
oneTwo("abcdefghijklkmnopqrstuvwxyz1 234567890" ) → "bcaefdhigkljmnkpqostrvwuy zx23156489 7" "bcaefdhigkljmnkpqostrvwuy zx23156489 7" OK
oneTwo("abcdefghijklkmnopqrstuvwxyz1 23456789") → "bcaefdhigkljmnkpqostrvwuy zx23156489 7" "bcaefdhigkljmnkpqostrvwuy zx23156489 7" OK
oneTwo("abcdefghijklkmnopqrstuvwxyz1 2345678") → "bcaefdhigkljmnkpqostrvwuy zx231564" "bcaefdhigkljmnkpqostrvwuy zx231564" OK
other tests
OK
ASKER
public String oneTwo(String str) {
return str.replaceAll("(.)(..)|.+","$2$1");
}
can you please advise on above?
we are doing repalce all on string with $2 and $1 which are end characters?
not clear on . and ..
You can also eliminate some of the local variables:
You can even combine the two append statements into one by using a '+' for concatenation of strings if you like:
public String oneTwo(String str) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<=str.length()-3; i=i+3) {
sb.append(str.substring(i+1,i+3));
sb.append(str.charAt(i));
}
return sb.toString();
}
You can even combine the two append statements into one by using a '+' for concatenation of strings if you like:
sb.append(str.substring(i+1,i+3) + str.charAt(i));
In a regular expression
. matches one character
.. matches two characters
. matches one character
.. matches two characters
ASKER
public String oneTwo(String str) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= str.length() - 3; i+=3) {
// String next = str.substring( i + 1, i + 3);
// String get1 = str.substring( i, i + 1);
// sb.append(next).append(get1);
sb.append(str.substring(i+1,i+3) + str.charAt(i));
}
return sb.toString();
}
above also passed all tests
ASKER
what is meaning of $2 and $1 and what are we doing in below code? please advise
str.replaceAll("(.)(..)|.+ ","$2$1");
str.replaceAll("(.)(..)|.+
These are regular expressions: $2 means the second grouping in this case (..) and $1 means the first grouping in this case (.).
ASKER
return str.replaceAll("(.)(..)|.+","$2$1");
what is meaning of above line as a whole?
how to decipher it?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
is capture group samne as match group as below?
http://regexone.com/lesson/capturing_groups
where can i find more reading, information on capture group in simple, clear words?
http://regexone.com/lesson/capturing_groups
where can i find more reading, information on capture group in simple, clear words?
ASKER
then capture the next three characters and do the same thing.
where we told to do same thing by capturing next 3 characters in
str = str.replaceAll("(.)(..)|.+
return str;
(.) means group the first character as the first captured group
(..) means group the next two characters as the second captured group
| means 'or'
.+ means one or more characters
$2 refers to the second captured group
$1 refers to the first captured group
We are told by the All in replaceAll
ASKER
got it.
why we need double quotes as highlighted?
str = str.replaceAll([b]"[/b](.)(..)|.+","$2$1[b]"[/b]);
why we need double quotes as highlighted?
ASKER
http://www.tutorialspoint.com/java/java_string_replaceall.htm
i see you have first one regex , and then second parameter replacement
i see you have first one regex , and then second parameter replacement
why we need double quotesIn Java, a string literal consists of zero or more characters enclosed in double quotes. https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5
I also think you need to increment by 3 and not 2. Additionally, your append statements need to first append the second two letters of the trio then append the first letter of the trio. So your code should look more like -
StringBuilder sb = new StringBuilder;
for (int I = 0; I <= str.length() - 3; I+=3) {
String next2 = str.substring( i + 1, I + 3);
String get1 = str.substring( i, i + 1);
sb.append(next).append(get
}
return sb.toString();