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; }
public String mirrorEnds(String string) {
ReplyDeleteString 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;
}
public String mirrorEnds(String string) {
ReplyDeleteint 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;
}
More concisely and without any if statement:
ReplyDeletepublic 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();
}
Seems like an overly complicated solution. Three lines O(n/2) solution here:
ReplyDeletepublic 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);
}
public String mirrorEnds(String string) {
ReplyDeleteString 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;
}
another with while loop
ReplyDeleteString 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;
public String mirrorEnds(String string) {
ReplyDeleteString 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;
}
public String mirrorEnds(String string) {
ReplyDeletefor (int i = 0; i < string.length(); ++i){
if (string.charAt(i) != string.charAt(string.length() - i - 1)){
return string.substring(0, i);
}
}
return string;
}
here u have python xd
ReplyDeletedef 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
public String mirrorEnds(String string) {
ReplyDeleteString 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;
}
public String mirrorEnds(String string) {
ReplyDeleteString 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;
}
public String mirrorEnds(String string) {
ReplyDeleteString 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;
}