Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

oneTwo java challenge

Hi,
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();
  
}

Open in new window

I am getting below result
Compile 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
Avatar of awking00
awking00
Flag of United States of America image

You were close but the Java syntax for incrementing by more than one is like int i = i+=n.
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(get1);
}
return sb.toString();
Avatar of gudii9

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();
  
}

Open in new window


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("abcdefghijklkmnopqrstuvwxyz1234567890") → "bcaefdhigkljmnkpqostrvwuyzx231564897"      "abcdefghijklkmnopqrstuvwxyz123456789"      X         
oneTwo("abcdefghijklkmnopqrstuvwxyz123456789") → "bcaefdhigkljmnkpqostrvwuyzx231564897"      "abcdefghijklkmnopqrstuvwxyz123456789"      X         
oneTwo("abcdefghijklkmnopqrstuvwxyz12345678") → "bcaefdhigkljmnkpqostrvwuyzx231564"      "abcdefghijklkmnopqrstuvwxyz123456"      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.
Avatar of gudii9

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();
  
}

Open in new window


above give below results

Expected      Run            
oneTwo("abc") → "bca"      "cabc"      X         
oneTwo("tca") → "cat"      "atca"      X         
oneTwo("tcagdo") → "catdog"      "atcagdootcagdogdo"      X         
oneTwo("chocolate") → "hocolctea"      "ochocolatelchocolatecolateechocolatecolateate"      X         
oneTwo("1234567890") → "231564897"      "312345678906123456789045678909123456789045678907890"      X         
oneTwo("xabxabxabxabxabxabxab") → "abxabxabxabxabxabxabx"      "bxabxabxabxabxabxabxabbxabxabxabxabxabxabxabxabxabxabxabxabxabbxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabbxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabbxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabbxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabbxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxabxab"      X         
oneTwo("abcdefx") → "bcaefd"      "cabcdefxfabcdefxdefx"      X         
oneTwo("abcdefxy") → "bcaefd"      "cabcdefxyfabcdefxydefxy"      X         
oneTwo("abcdefxyz") → "bcaefdyzx"      "cabcdefxyzfabcdefxyzdefxyzzabcdefxyzdefxyzxyz"      X         
oneTwo("") → ""      ""      OK         
oneTwo("x") → ""      ""      OK         
oneTwo("xy") → ""      ""      OK         
oneTwo("xyz") → "yzx"      "zxyz"      X         
oneTwo("abcdefghijklkmnopqrstuvwxyz1234567890") → "bcaefdhigkljmnkpqostrvwuyzx231564897"      "cabcdefghijklkmnopqrstuvwxyz1234567890fabcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890iabcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890ghijklkmnopqrstuvwxyz1234567890labcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890ghijklkmnopqrstuvwxyz1234567890jklkmnopqrstuvwxyz1234567890nabcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890ghijklkmnopqrstuvwxyz1234567890jklkmnopqrstuvwxyz1234567890kmnopqrstuvwxyz1234567890qabcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890ghijklkmnopqrstuvwxyz1234567890jklkmnopqrstuvwxyz1234567890kmnopqrstuvwxyz1234567890opqrstuvwxyz1234567890tabcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890ghijklkmnopqrstuvwxyz1234567890jklkmnopqrstuvwxyz1234567890kmnopqrstuvwxyz1234567890opqrstuvwxyz1234567890rstuvwxyz1234567890wabcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890ghijklkmnopqrstuvwxyz1234567890jklkmnopqrstuvwxyz1234567890kmnopqrstuvwxyz1234567890opqrstuvwxyz1234567890rstuvwxyz1234567890uvwxyz1234567890zabcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890ghijklkmnopqrstuvwxyz1234567890jklkmnopqrstuvwxyz1234567890kmnopqrstuvwxyz1234567890opqrstuvwxyz1234567890rstuvwxyz1234567890uvwxyz1234567890xyz12345678903abcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890ghijklkmnopqrstuvwxyz1234567890jklkmnopqrstuvwxyz1234567890kmnopqrstuvwxyz1234567890opqrstuvwxyz1234567890rstuvwxyz1234567890uvwxyz1234567890xyz123456789012345678906abcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890ghijklkmnopqrstuvwxyz1234567890jklkmnopqrstuvwxyz1234567890kmnopqrstuvwxyz1234567890opqrstuvwxyz1234567890rstuvwxyz1234567890uvwxyz1234567890xyz1234567890123456789045678909abcdefghijklkmnopqrstuvwxyz1234567890defghijklkmnopqrstuvwxyz1234567890ghijklkmnopqrstuvwxyz1234567890jklkmnopqrstuvwxyz1234567890kmnopqrstuvwxyz1234567890opqrstuvwxyz1234567890rstuvwxyz1234567890uvwxyz1234567890xyz1234567890123456789045678907890"      X         
oneTwo("abcdefghijklkmnopqrstuvwxyz123456789") → "bcaefdhigkljmnkpqostrvwuyzx231564897"      "cabcdefghijklkmnopqrstuvwxyz123456789fabcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789iabcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789ghijklkmnopqrstuvwxyz123456789labcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789ghijklkmnopqrstuvwxyz123456789jklkmnopqrstuvwxyz123456789nabcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789ghijklkmnopqrstuvwxyz123456789jklkmnopqrstuvwxyz123456789kmnopqrstuvwxyz123456789qabcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789ghijklkmnopqrstuvwxyz123456789jklkmnopqrstuvwxyz123456789kmnopqrstuvwxyz123456789opqrstuvwxyz123456789tabcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789ghijklkmnopqrstuvwxyz123456789jklkmnopqrstuvwxyz123456789kmnopqrstuvwxyz123456789opqrstuvwxyz123456789rstuvwxyz123456789wabcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789ghijklkmnopqrstuvwxyz123456789jklkmnopqrstuvwxyz123456789kmnopqrstuvwxyz123456789opqrstuvwxyz123456789rstuvwxyz123456789uvwxyz123456789zabcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789ghijklkmnopqrstuvwxyz123456789jklkmnopqrstuvwxyz123456789kmnopqrstuvwxyz123456789opqrstuvwxyz123456789rstuvwxyz123456789uvwxyz123456789xyz1234567893abcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789ghijklkmnopqrstuvwxyz123456789jklkmnopqrstuvwxyz123456789kmnopqrstuvwxyz123456789opqrstuvwxyz123456789rstuvwxyz123456789uvwxyz123456789xyz1234567891234567896abcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789ghijklkmnopqrstuvwxyz123456789jklkmnopqrstuvwxyz123456789kmnopqrstuvwxyz123456789opqrstuvwxyz123456789rstuvwxyz123456789uvwxyz123456789xyz1234567891234567894567899abcdefghijklkmnopqrstuvwxyz123456789defghijklkmnopqrstuvwxyz123456789ghijklkmnopqrstuvwxyz123456789jklkmnopqrstuvwxyz123456789kmnopqrstuvwxyz123456789opqrstuvwxyz123456789rstuvwxyz123456789uvwxyz123456789xyz123456789123456789456789789"      X         
oneTwo("abcdefghijklkmnopqrstuvwxyz12345678") → "bcaefdhigkljmnkpqostrvwuyzx231564"      "cabcdefghijklkmnopqrstuvwxyz12345678fabcdefghijklkmnopqrstuvwxyz12345678defghijklkmnopqrstuvwxyz12345678iabcdefghijklkmnopqrstuvwxyz12345678defghijklkmnopqrstuvwxyz12345678ghijklkmnopqrstuvwxyz12345678labcdefghijklkmnopqrstuvwxyz12345678defghijklkmnopqrstuvwxyz12345678ghijklkmnopqrstuvwxyz12345678jklkmnopqrstuvwxyz12345678nabcdefghijklkmnopqrstuvwxyz12345678defghijklkmnopqrstuvwxyz12345678ghijklkmnopqrstuvwxyz12345678jklkmnopqrstuvwxyz12345678kmnopqrstuvwxyz12345678qabcdefghijklkmnopqrstuvwxyz12345678defghijklkmnopqrstuvwxyz12345678ghijklkmnopqrstuvwxyz12345678jklkmnopqrstuvwxyz12345678kmnopqrstuvwxyz12345678opqrstuvwxyz12345678tabcdefghijklkmnopqrstuvwxyz12345678defghijklkmnopqrstuvwxyz12345678ghijklkmnopqrstuvwxyz12345678jklkmnopqrstuvwxyz12345678kmnopqrstuvwxyz12345678opqrstuvwxyz12345678rstuvwxyz12345678wabcdefghijklkmnopqrstuvwxyz12345678defghijklkmnopqrstuvwxyz12345678ghijklkmnopqrstuvwxyz12345678jklkmnopqrstuvwxyz12345678kmnopqrstuvwxyz12345678opqrstuvwxyz12345678rstuvwxyz12345678uvwxyz12345678zabcdefghijklkmnopqrstuvwxyz12345678defghijklkmnopqrstuvwxyz12345678ghijklkmnopqrstuvwxyz12345678jklkmnopqrstuvwxyz12345678kmnopqrstuvwxyz12345678opqrstuvwxyz12345678rstuvwxyz12345678uvwxyz12345678xyz123456783abcdefghijklkmnopqrstuvwxyz12345678defghijklkmnopqrstuvwxyz12345678ghijklkmnopqrstuvwxyz12345678jklkmnopqrstuvwxyz12345678kmnopqrstuvwxyz12345678opqrstuvwxyz12345678rstuvwxyz12345678uvwxyz12345678xyz12345678123456786abcdefghijklkmnopqrstuvwxyz12345678defghijklkmnopqrstuvwxyz12345678ghijklkmnopqrstuvwxyz12345678jklkmnopqrstuvwxyz12345678kmnopqrstuvwxyz12345678opqrstuvwxyz12345678rstuvwxyz12345678uvwxyz12345678xyz123456781234567845678"      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.
Avatar of gudii9

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();

}

Open in new window


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"      "abxabxxabxxxabxxxxabxxxxxabxxxxxxabxxxxxxx"      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("abcdefghijklkmnopqrstuvwxyz1234567890") → "bcaefdhigkljmnkpqostrvwuyzx231564897"      "bcaefadhiadgkladgjmnadgjkpqadgjkostadgjkorvwadgjkoruyzadgjkorux23adgjkorux156adgjkorux1489adgjkorux147"      X         
oneTwo("abcdefghijklkmnopqrstuvwxyz123456789") → "bcaefdhigkljmnkpqostrvwuyzx231564897"      "bcaefadhiadgkladgjmnadgjkpqadgjkostadgjkorvwadgjkoruyzadgjkorux23adgjkorux156adgjkorux1489adgjkorux147"      X         
oneTwo("abcdefghijklkmnopqrstuvwxyz12345678") → "bcaefdhigkljmnkpqostrvwuyzx231564"      "bcaefadhiadgkladgjmnadgjkpqadgjkostadgjkorvwadgjkoruyzadgjkorux23adgjkorux156adgjkorux14"      X         
other tests
Avatar of gudii9

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();
  
}

Open in new window


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"      "abxabxxabxxxabxxxxabxxxxxabxxxxxxabxxxxxxx"      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("abcdefghijklkmnopqrstuvwxyz1234567890") → "bcaefdhigkljmnkpqostrvwuyzx231564897"      "bcabcaabcaaabcaaaabcaaaaabcaaaaaabcaaaaaaabcaaaaaaaabcaaaaaaaaabcaaaaaaaaaabcaaaaaaaaaaabcaaaaaaaaaaaa"      X         
oneTwo("abcdefghijklkmnopqrstuvwxyz123456789") → "bcaefdhigkljmnkpqostrvwuyzx231564897"      "bcabcaabcaaabcaaaabcaaaaabcaaaaaabcaaaaaaabcaaaaaaaabcaaaaaaaaabcaaaaaaaaaabcaaaaaaaaaaabcaaaaaaaaaaaa"      X         
oneTwo("abcdefghijklkmnopqrstuvwxyz12345678") → "bcaefdhigkljmnkpqostrvwuyzx231564"      "bcabcaabcaaabcaaaabcaaaaabcaaaaaabcaaaaaaabcaaaaaaaabcaaaaaaaaabcaaaaaaaaaabcaaaaaaaaaaa"      X         
other tests
SOLUTION
Avatar of awking00
awking00
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Go back and run those System.out.println(sb.toString()) statements after creation of each StringBuilder to show what is being returned for each.
Avatar of gudii9

ASKER

for string length 3 and less works fine but more than 3 fails
Avatar of gudii9

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();
		  
		}

}

Open in new window


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
Avatar of gudii9

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();
		  
		}

}

Open in new window


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
Avatar of gudii9

ASKER

value of sb3--->catdotg

not sure why t coming before g
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

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();
		  
		}

}

Open in new window


above passed all tests
what is difference between below
sb=sb.append(twoChar).append(single);

and below
sb.append(twoChar).append(single);

also how many times we can chain append append append

sb=sb.append(twoChar).append(single).append("something1").sppend("something2")....//how many maximum number of appends we can use?
Avatar of gudii9

ASKER

public String oneTwo(String str) {
 return str.replaceAll("(.)(..)|.+","$2$1");
}

Open in new window


not clear on above.

please advise
Avatar of gudii9

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();
  
}

Open in new window


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("abcdefghijklkmnopqrstuvwxyz1234567890") → "bcaefdhigkljmnkpqostrvwuyzx231564897"      "bcaefdhigkljmnkpqostrvwuyzx231564897"      OK         
oneTwo("abcdefghijklkmnopqrstuvwxyz123456789") → "bcaefdhigkljmnkpqostrvwuyzx231564897"      "bcaefdhigkljmnkpqostrvwuyzx231564897"      OK         
oneTwo("abcdefghijklkmnopqrstuvwxyz12345678") → "bcaefdhigkljmnkpqostrvwuyzx231564"      "bcaefdhigkljmnkpqostrvwuyzx231564"      OK         
other tests
OK      
Avatar of gudii9

ASKER

public String oneTwo(String str) {
 return str.replaceAll("(.)(..)|.+","$2$1");
}

Open in new window


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 ..
Avatar of ProgSysAdmin
ProgSysAdmin

You can also eliminate some of the local variables:

     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();
     }

Open in new window


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)); 

Open in new window

In a regular expression
. matches one character
.. matches two characters
Avatar of gudii9

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();
  
}

Open in new window

above also passed all tests
Avatar of gudii9

ASKER

what is meaning of $2 and $1 and what are we doing in below code? please advise

str.replaceAll("(.)(..)|.+","$2$1");
These are regular expressions: $2 means the second grouping in this case (..) and $1 means the first grouping in this case (.).
Avatar of gudii9

ASKER

return str.replaceAll("(.)(..)|.+","$2$1");

what is meaning of above line as a whole?

how to decipher it?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

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?
Avatar of gudii9

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("(.)(..)|.+","$2$1");
      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
Avatar of gudii9

ASKER

got it.

str = str.replaceAll([b]"[/b](.)(..)|.+","$2$1[b]"[/b]);

Open in new window


why we need double quotes as highlighted?
Avatar of gudii9

ASKER

http://www.tutorialspoint.com/java/java_string_replaceall.htm

i see you have first one regex , and then second parameter replacement
why we need double quotes
In 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