Skip to main content
Code Review

Return to Question

Tweeted twitter.com/#!/StackCodeReview/status/560581314399985664
Added a new example that includes the constraint of asking for more letters than the string contains.
Source Link

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front:

frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"
frontTimes("X", 3) → "XXX"

I've solved above problem using following code but I'm using a new StringBuilder to store intermediate string result. Does it really needed can I optimize and remove the for loop in the else?

public String frontTimes(String str, int n) {
 
 StringBuilder sb = new StringBuilder(str);
 StringBuilder newsb = new StringBuilder();
 
 if(sb.length() >= 3)
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.substring(0,3));
 }
 
 return newsb.toString(); 
 }
 else
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.toString());
 }
 
 return newsb.toString();
 
 }
 
 }

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front:

frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"

I've solved above problem using following code but I'm using a new StringBuilder to store intermediate string result. Does it really needed can I optimize and remove the for loop in the else?

public String frontTimes(String str, int n) {
 
 StringBuilder sb = new StringBuilder(str);
 StringBuilder newsb = new StringBuilder();
 
 if(sb.length() >= 3)
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.substring(0,3));
 }
 
 return newsb.toString(); 
 }
 else
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.toString());
 }
 
 return newsb.toString();
 
 }
 
 }

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front:

frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"
frontTimes("X", 3) → "XXX"

I've solved above problem using following code but I'm using a new StringBuilder to store intermediate string result. Does it really needed can I optimize and remove the for loop in the else?

public String frontTimes(String str, int n) {
 
 StringBuilder sb = new StringBuilder(str);
 StringBuilder newsb = new StringBuilder();
 
 if(sb.length() >= 3)
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.substring(0,3));
 }
 
 return newsb.toString(); 
 }
 else
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.toString());
 }
 
 return newsb.toString();
 
 }
 
 }
added 11 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

new Usage of StringBuilder is really needed?for returning copies of a string

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;front:

frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"

I've solved above problem using following code but I'm using a new StringBuilderStringBuilder to store intermediate string result, does. Does it really needed can I optimize and remove forthe for loop in elsethe else?

public String frontTimes(String str, int n) {
 
 StringBuilder sb = new StringBuilder(str);
 StringBuilder newsb = new StringBuilder();
 
 if(sb.length() >= 3)
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.substring(0,3));
 }
 
 return newsb.toString(); 
 }
 else
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.toString());
 }
 
 return newsb.toString();
 
 }
 
 }

new StringBuilder is really needed?

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;

frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"

I've solved above problem using following code but I'm using a new StringBuilder to store intermediate string result, does it really needed can I optimize and remove for loop in else?

public String frontTimes(String str, int n) {
 
 StringBuilder sb = new StringBuilder(str);
 StringBuilder newsb = new StringBuilder();
 
 if(sb.length() >= 3)
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.substring(0,3));
 }
 
 return newsb.toString(); 
 }
 else
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.toString());
 }
 
 return newsb.toString();
 
 }
 
 }

Usage of StringBuilder for returning copies of a string

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front:

frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"

I've solved above problem using following code but I'm using a new StringBuilder to store intermediate string result. Does it really needed can I optimize and remove the for loop in the else?

public String frontTimes(String str, int n) {
 
 StringBuilder sb = new StringBuilder(str);
 StringBuilder newsb = new StringBuilder();
 
 if(sb.length() >= 3)
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.substring(0,3));
 }
 
 return newsb.toString(); 
 }
 else
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.toString());
 }
 
 return newsb.toString();
 
 }
 
 }
added 5 characters in body
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396
Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;
frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;

frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"

I've solved above problem using following code but I'm using a new StringBuilder to store intermediate string result, does it really needed can I optimize and remove for loop in else?

public String frontTimes(String str, int n) {
 
 StringBuilder sb = new StringBuilder(str);
 StringBuilder newsb = new StringBuilder();
 
 if(sb.length() >= 3)
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.substring(0,3));
 }
 
 return newsb.toString(); 
 }
 else
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.toString());
 }
 
 return newsb.toString();
 
 }
 
 }
Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;
frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"

I've solved above problem using following code but I'm using a new StringBuilder to store intermediate string result, does it really needed can I optimize and remove for loop in else?

public String frontTimes(String str, int n) {
 
 StringBuilder sb = new StringBuilder(str);
 StringBuilder newsb = new StringBuilder();
 
 if(sb.length() >= 3)
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.substring(0,3));
 }
 
 return newsb.toString(); 
 }
 else
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.toString());
 }
 
 return newsb.toString();
 
 }
 
 }

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;

frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"

I've solved above problem using following code but I'm using a new StringBuilder to store intermediate string result, does it really needed can I optimize and remove for loop in else?

public String frontTimes(String str, int n) {
 
 StringBuilder sb = new StringBuilder(str);
 StringBuilder newsb = new StringBuilder();
 
 if(sb.length() >= 3)
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.substring(0,3));
 }
 
 return newsb.toString(); 
 }
 else
 {
 for(int i = 0; i < n; i++)
 {
 newsb.append(sb.toString());
 }
 
 return newsb.toString();
 
 }
 
 }
Source Link
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /