public String oneTwo(String str) { String a = ""; for(int i = 0; i < str.length()-2; i+=3){ a = a + str.charAt(i+1) + str.charAt(i+2)+ str.charAt(i); } return a; }
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.
public String oneTwo(String str) { int len = str.length(); String[] arr = new String[len/3]; if (len % 3 != 0) { int mod1 = len % 3; str = str.substring(0,len-mod1); } int j = 0; for (int i = 0; i < len/3; i++) { arr[i] = str.substring(j,j+3); j += 3; } String strAll=""; for (int i = 0; i < len / 3; i++) { strAll += arr[i].substring(1,3) + arr[i].substring(0,1); } return strAll; }
The above mentioned example is a sample test case for your understanding. The program will be tested on other secret test cases in the backend.Output format should be maintained to be scored properly.Output format should be maintained to be scored properly Make sure you click the SUBMIT button to save and submit your answer.
If you like our content, feel free to follow me to stay updated.
Subscribe
Upload Material
Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?
for "oneTwo":
ReplyDeletepublic String oneTwo(String str) {
String result = "";
for (int i=0; i<str.length()-2; i+=3){
result = result + str.substring(i+1, i+3) + str.charAt(i);
}
return result;
}
thank you boss
Deletepublic String oneTwo(String str) {
ReplyDeleteString tmp = "";
int len = str.length();
for (int i = 0; i<len-2; i+=3){
tmp += str.substring(i+1,i+3)+str.substring(i,i+1);
}
return tmp;
}
Recursion for "String-2 > oneTwo"
ReplyDeletepublic String oneTwo(String str) {
if( str.length() < 3 ) return "";
return ""+str.charAt(1)+str.charAt(2)+str.charAt(0)+oneTwo(str.substring(3));
}
public String oneTwo(String str) {
ReplyDeleteString a = "";
for(int i = 0; i < str.length()-2; i+=3){
a = a + str.charAt(i+1) + str.charAt(i+2)+ str.charAt(i);
}
return a;
}
public boolean xyBalance(String str) {
ReplyDeleteint len=str.length();
boolean r= true;
if(len>=1){
for(int i=0;i<len;i++){
if(str.charAt(i)=='x'){
r=false;
for(int j=i+1;j<len;j++)
if(str.charAt(j)=='y')
r=true;
}
}
}
return r;
}
ReplyDeleteGiven 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.
public String oneTwo(String str) {
String str1="";
if(str.length()>2)
{
for(int i=0;i<str.length()-2;i=i+3)
str1+=str.substring(i+1,i+3)+str.charAt(i);
}
return str1;
}
public String oneTwo(String str) {
ReplyDeleteint len = str.length();
String[] arr = new String[len/3];
if (len % 3 != 0) {
int mod1 = len % 3;
str = str.substring(0,len-mod1);
}
int j = 0;
for (int i = 0; i < len/3; i++) {
arr[i] = str.substring(j,j+3);
j += 3;
}
String strAll="";
for (int i = 0; i < len / 3; i++) {
strAll += arr[i].substring(1,3) + arr[i].substring(0,1);
}
return strAll;
}
Coding Question Mark For Review
ReplyDeleteProblem Statement
Given an array count the number of even elements in an array.
Complete the functions countEvens() to get the expected output.
Only edit the function and not the existing code.
Note
Edit only the function and not the existing code.
Sample Testcases
countEvens([2, 1, 2, 3, 4]) → 3
countEvens([2, 2, 0]) → 3
countEvens([1, 3, 5]) → 0
--------------------------------------------------------------------------------
The above mentioned example is a sample test case for your understanding. The program will be tested on other secret test cases in the backend.Output format should be maintained to be scored properly.Output format should be maintained to be scored properly Make sure you click the SUBMIT button to save and submit your answer.