terekhovm
Reputation Top 1%
terekhovm
3 Snippets  (199th place)
Published
1 Channel
Created
3 Channels
Following
160 points  (188th place)
Reputation
Junior Code Generator
Serious Code Generator
Junior Publisher
Junior Topic Hub
Junior Trend Maker
Junior Popular Coder
Junior Autobiographer
Serious Autobiographer
Junior Famous Coder

## Recent Snippets See all snippets by terekhovm

### Поиск длиннейшей подстроки-палиндрома

longest palindrome in given arg str
```import java.util.Arrays;

//call like findLongestPalindrome("heysysoop");  returns "sys"
public class ManachersAlgorithm {

public static String findLongestPalindrome(String s) {
if (s==null || s.length()==0)
return "";

int[] p = new int[s2.length];
int c = 0, r = 0; // Here the first element in s2 has been processed.
int m = 0, n = 0; // The walking indices to compare if two elements are the same
for (int i = 1; i<s2.length; i++) {
if (i>r) {
p[i] = 0; m = i-1; n = i+1;
} else {
int i2 = c*2-i;
if (p[i2]<(r-i)) {
p[i] = p[i2];
m = -1; // This signals bypassing the while loop below.
} else {
p[i] = r-i;
n = r+1; m = i*2-n;
}
}
while (m>=0 && n<s2.length && s2[m]==s2[n]) {
p[i]++; m--; n++;
}
if ((i+p[i])>r) {
c = i; r = i+p[i];
}
}
int len = 0; c = 0;
for (int i = 1; i<s2.length; i++) {
if (len<p[i]) {
len = p[i]; c = i;
}
}
char[] ss = Arrays.copyOfRange(s2, c-len, c+len+1);
return String.valueOf(removeBoundaries(ss));
}

private static char[] addBoundaries(char[] cs) {
if (cs==null || cs.length==0)
return "||".toCharArray();

char[] cs2 = new char[cs.length*2+1];
for (int i = 0; i<(cs2.length-1); i = i+2) {
cs2[i] = '|';
cs2[i+1] = cs[i/2];
}
cs2[cs2.length-1] = '|';
return cs2;
}

private static char[] removeBoundaries(char[] cs) {
if (cs==null || cs.length<3)
return "".toCharArray();

char[] cs2 = new char[(cs.length-1)/2];
for (int i = 0; i<cs2.length; i++) {
cs2[i] = cs[i*2+1];
}
return cs2;
}
}```
;

### equalTo() - override

equalTo() - override descr
```import org.apache.commons.lang3.builder.EqualsBuilder;

class Cake {
String type;

public Cake(String type) {
this.type = type;

}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Cake)) {
return false;
} else if (obj == this) {
return true;
}

Cake rhs = (Cake) obj;
return new EqualsBuilder().append(type, rhs.type).isEquals();

}

}```
;

### assertThat

assertThat example
```import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;

public class MyTest {

@Test
public void testCake() throws Exception {
Cake my1 = new Cake("lemon");
Cake my2 = new Cake("lemon");
assertThat(my1, equalTo(my2)); //Cake.equalTo must be impl by you
}

}```
;