Skip to main content
Code Review

Return to Question

fixed indentation
Source Link
IEatBagels
  • 12.7k
  • 3
  • 48
  • 99

Problem statement:

Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

Examples:

  • mirrorEnds("abXYZba") → "ab"
  • mirrorEnds("abca") → "a"
  • mirrorEnds("aba") → "aba"

Below is my solution to the problem in java:

public String mirrorEnds(String string) {
 final int len = string.length();
 final int half = len / 2;
 String result = "";

 for (int i = 0; i < half; i++) {
 if (string.charAt(i) != string.charAt(len -1 -i)) {
 break; 
 } else {
 result += string.substring(i, i + 1);
 }
 }
 return result.length() == half ? string : result;
}

Is it safe to say that in terms of time complexity the solution is optimal already? Any other review comments are also welcome.

p.s. - please ignore the indenting!

Problem statement:

Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

Examples:

  • mirrorEnds("abXYZba") → "ab"
  • mirrorEnds("abca") → "a"
  • mirrorEnds("aba") → "aba"

Below is my solution to the problem in java:

public String mirrorEnds(String string) {
 final int len = string.length();
 final int half = len / 2;
 String result = "";

 for (int i = 0; i < half; i++) {
 if (string.charAt(i) != string.charAt(len -1 -i)) {
 break; 
 } else {
 result += string.substring(i, i + 1);
 }
 }
 return result.length() == half ? string : result;
}

Is it safe to say that in terms of time complexity the solution is optimal already? Any other review comments are also welcome.

p.s. - please ignore the indenting!

Problem statement:

Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

Examples:

  • mirrorEnds("abXYZba") → "ab"
  • mirrorEnds("abca") → "a"
  • mirrorEnds("aba") → "aba"

Below is my solution to the problem in java:

public String mirrorEnds(String string) {
 final int len = string.length();
 final int half = len / 2;
 String result = "";
 for (int i = 0; i < half; i++) {
 if (string.charAt(i) != string.charAt(len -1 -i)) {
 break; 
 } else {
 result += string.substring(i, i + 1);
 }
 }
 return result.length() == half ? string : result;
}

Is it safe to say that in terms of time complexity the solution is optimal already? Any other review comments are also welcome.

Became Hot Network Question

Problem statement:

Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

mirrorEnds("abXYZba") → "ab" mirrorEnds("abca") → "a" mirrorEnds("aba") → "aba"Examples:

  • mirrorEnds("abXYZba") → "ab"
  • mirrorEnds("abca") → "a"
  • mirrorEnds("aba") → "aba"

Below is my solution to the problem in java:

 public String mirrorEnds(String string) {
 final int len = string.length();
 final int half = len / 2;
 String result = "";
 
 for (int i = 0; i < half; i++) {
 if (string.charAt(i) != string.charAt(len -1 -i)) {
 break; 
 } else {
 result += string.substring(i, i + 1);
 }
 }
 return result.length() == half ? string : result;
 }

Is it safe to say that in terms of time complexity the solution is optimal already? Any other review comments are also welcome.

p.s. - please ignore the indenting!

Problem statement:

Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

mirrorEnds("abXYZba") → "ab" mirrorEnds("abca") → "a" mirrorEnds("aba") → "aba"

Below is my solution to the problem in java:

 public String mirrorEnds(String string) {
 final int len = string.length();
 final int half = len / 2;
 String result = "";
 
 for (int i = 0; i < half; i++) {
 if (string.charAt(i) != string.charAt(len -1 -i)) {
 break; 
 } else {
 result += string.substring(i, i + 1);
 }
 }
 return result.length() == half ? string : result;
 }

Is it safe to say that in terms of time complexity the solution is optimal already? Any other review comments are also welcome.

p.s. - please ignore the indenting!

Problem statement:

Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

Examples:

  • mirrorEnds("abXYZba") → "ab"
  • mirrorEnds("abca") → "a"
  • mirrorEnds("aba") → "aba"

Below is my solution to the problem in java:

 public String mirrorEnds(String string) {
 final int len = string.length();
 final int half = len / 2;
 String result = "";
 
 for (int i = 0; i < half; i++) {
 if (string.charAt(i) != string.charAt(len -1 -i)) {
 break; 
 } else {
 result += string.substring(i, i + 1);
 }
 }
 return result.length() == half ? string : result;
 }

Is it safe to say that in terms of time complexity the solution is optimal already? Any other review comments are also welcome.

p.s. - please ignore the indenting!

Tweeted twitter.com/StackCodeReview/status/1250801185294073864
added 14 characters in body
Source Link
Anirudh
  • 872
  • 2
  • 13
  • 28

Problem statement:

Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

mirrorEnds("abXYZba") → "ab" mirrorEnds("abca") → "a" mirrorEnds("aba") → "aba"

Below is my solution to the problem in java:

 public String mirrorEnds(String string) {
 final int len = string.length();
 final int half = len / 2;
 String result = "";
 
 for (int i = 0; i < half; i++) {
 if (string.charAt(i) != string.charAt(len -1 -i)) {
 break; 
 } else {
 result += string.substring(i, i + 1);
 }
 }
 return result.length() == half ? string : result;
 }

Is it safe to say that in terms of time complexity the solution is optimal already? Any other review comments are also welcome.

p.s. - please ignore the indenting!

Problem statement:

Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

mirrorEnds("abXYZba") → "ab" mirrorEnds("abca") → "a" mirrorEnds("aba") → "aba"

Below is my solution to the problem in java:

 public String mirrorEnds(String string) {
 final int len = string.length();
 final int half = len / 2;
 String result = "";
 
 for (int i = 0; i < half; i++) {
 if (string.charAt(i) != string.charAt(len -1 -i)) {
 break; 
 } else {
 result += string.substring(i, i + 1);
 }
 }
 return result.length() == half ? string : result;
 }

Is it safe to say that in terms of time complexity the solution is optimal already? Any other review comments are also welcome.

p.s. - please ignore the indenting!

Problem statement:

Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

mirrorEnds("abXYZba") → "ab" mirrorEnds("abca") → "a" mirrorEnds("aba") → "aba"

Below is my solution to the problem in java:

 public String mirrorEnds(String string) {
 final int len = string.length();
 final int half = len / 2;
 String result = "";
 
 for (int i = 0; i < half; i++) {
 if (string.charAt(i) != string.charAt(len -1 -i)) {
 break; 
 } else {
 result += string.substring(i, i + 1);
 }
 }
 return result.length() == half ? string : result;
 }

Is it safe to say that in terms of time complexity the solution is optimal already? Any other review comments are also welcome.

p.s. - please ignore the indenting!

Source Link
Anirudh
  • 872
  • 2
  • 13
  • 28
Loading
lang-java

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