Java > String-3 > mirrorEnds (CodingBat Solution)

Problem:

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"


Solution:

public String mirrorEnds(String string) {
  int len = string.length();
  String fin = "";
  String tmp1 = "";
  String tmp2 = "";

  
  for (int i = 0; i < len; i++) {
    tmp1 += string.substring(i,i+1);
    tmp2 = "";
    for (int j = tmp1.length()-1; j >= 0; j--) {
      tmp2 += tmp1.substring(j,j+1);
      if (tmp2.equals(string.substring(len-i-1,len)))
        fin = tmp1;
    }
  }
  return fin;
}


12 comments :

  1. public String mirrorEnds(String string) {
    String result = "";
    int len = string.length();

    for (int i = 0; i < len; i++) {
    if (string.charAt(i) == string.charAt(len-1-i))
    result += string.charAt(i);
    else
    return result;
    }
    return result;
    }

    ReplyDelete
  2. public String mirrorEnds(String string) {
    int length = string.length();
    if(length<2){
    return string;
    }

    StringBuilder sbr = new StringBuilder();

    // iterating string from both end
    for(int i=0, j=length -1; j>=0; i++, j--){
    if(string.charAt(i) == string.charAt(j)){
    sbr.append(string.charAt(i));
    }
    else{
    // if not equal
    break;
    }
    }



    String output = sbr.toString();
    return output;
    }

    ReplyDelete
  3. More concisely and without any if statement:

    public String mirrorEnds(String string) {
    StringBuilder res=new StringBuilder();
    int n=string.length();
    for (int i=0 ; i < n && string.charAt(i)==string.charAt(n-i-1) ; ++i)
    res.append(string.charAt(i));
    return res.toString();
    }

    ReplyDelete
  4. Seems like an overly complicated solution. Three lines O(n/2) solution here:

    public String mirrorEnds(String s) {
    int i = -1;
    while(++i < s.length() && s.charAt(i) == s.charAt(s.length()-i-1));
    return s.substring(0,i);
    }

    ReplyDelete
  5. public String mirrorEnds(String string) {
    String result = "";
    for(int i=1;i<string.length()+1;i++){
    if(string.charAt(i-1)==string.charAt(string.length()-i)){
    result = result + string.charAt(i-1);
    } else return result;
    }
    return result;
    }

    ReplyDelete
  6. another with while loop

    String temp = "";

    int i = 0;
    while (i < string.length() && string.substring(i, i + 1).equals(string.substring(string.length() - (i + 1), string.length() - i))) {

    temp += string.substring(i, i + 1);

    i++;
    }
    return temp;

    ReplyDelete
  7. public String mirrorEnds(String string) {
    String mirror = "";
    for (int i = 0; i < string.length(); i++)
    {
    for (int j = string.length() - 1; j >= 0; j--)
    {
    if (i < string.length() && string.charAt(j) == string.charAt(i))
    {
    mirror += string.charAt(j);
    i++;
    }
    else
    {
    return mirror;
    }
    }
    }
    return mirror;
    }

    ReplyDelete
  8. public String mirrorEnds(String string) {
    for (int i = 0; i < string.length(); ++i){
    if (string.charAt(i) != string.charAt(string.length() - i - 1)){
    return string.substring(0, i);
    }
    }

    return string;
    }

    ReplyDelete
  9. here u have python xd
    def mirrorEnds(s):
    a = ""
    s1 = s[::-1]
    tmp = ""
    for i in range(len(s)):
    tmp += s[i]
    if tmp == s1[:len(tmp)]:
    a = tmp
    return a

    ReplyDelete
  10. public String mirrorEnds(String string) {
    String res = "";
    String str1 = "";
    String str2 = "";
    int len = string.length();
    for (int i=0, j=len-1; i<len; i++, j--){
    str1 += string.charAt(i);
    str2 += string.charAt(j);
    if (str1.equals(str2))
    res = str1;
    }
    return res;

    }

    ReplyDelete
  11. public String mirrorEnds(String string) {
    String result = "";
    StringBuilder back = new StringBuilder(string);
    back.reverse();

    for (int i = 0; i < string.length(); i++) {
    if (string.substring(0, i + 1).equals(back.toString().substring(0, i + 1))) {
    result = string.substring(0, i + 1);
    }
    }
    return result;
    }

    ReplyDelete
  12. public String mirrorEnds(String string) {
    String a = "", b = "";
    String g = "";
    for(int i = string.length() - 1; i >= 0; i--){
    g += string.substring(i, i + 1);
    }
    if(string.equals(g)){
    return g;
    }
    for(int i = 0; i < string.length() / 2; i++){
    a = string.substring(i, i + 1) + a;
    if(a.equals(string.substring(string.length() - i -1))){
    b = a;
    }
    }
    String d = "";
    for(int i = b.length() - 1; i >= 0; i--){
    d += b.substring(i, i + 1);
    }
    return d;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

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?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact