Problem:
Given a string, consider the prefix string made of the first N chars of the string. Does that prefix string appear somewhere else in the string? Assume that the string is not empty and that N is in the range 1..str.length().
prefixAgain("abXYabc", 1) → true
prefixAgain("abXYabc", 2) → true
prefixAgain("abXYabc", 3) → false
Solution:
public boolean prefixAgain(String str, int n) { int len = str.length(); String prefix = str.substring(0,n); for (int i = n; i < len; i++) { if(n+i <= len) { if (prefix.equals(str.substring(i,n+i))) return true; } } return false; }
Smaller:
ReplyDeletepublic boolean prefixAgain(String str, int n) {
return str.lastIndexOf(str.substring(0, n)) != 0;
}
Nice!
Deleteor:
ReplyDeletepublic boolean prefixAgain(String str, int n) {
return (str.substring(n).indexOf(str.substring(0,n)) != -1);
}
public boolean prefixAgain(String str, int n) {
ReplyDeleteString substr = str.substring(n);
String pattern = str.substring(0, n);
return substr.contains(pattern);
}
public boolean prefixAgain(String str, int n) {
ReplyDeletereturn str.matches(str.substring(0, n) + "(.*)" + str.substring(0, n) + "(.*)");
}
public boolean prefixAgain(String str, int n) {
ReplyDeletereturn str.indexOf(str.substring(0, n), n) > -1;
}
public boolean prefixAgain(String str, int n) {
ReplyDeletereturn str.substring(n).contains(str.substring(0,n));
}
public boolean prefixAgain(String str, int n) {
ReplyDeletefor(int i=0; i<str.length()-n; i++)
{
if(str.substring(0,n).equals(str.substring(i+1,i+n+1)))
return true;
}
return false;
}
public boolean prefixAgain(String str, int n) {
ReplyDeletereturn str.substring(n).contains(str.substring(0,n));
}
public boolean prefixAgain(String str, int n) {
ReplyDeleteString result = str.substring(n);
String check = str.substring(0, n);
return result.contains(check);
}
public boolean prefixAgain(String str, int n) {
ReplyDeleteint sep = 0;
for (int i = n ; i <= str.length() - n ; i++){
if(str.substring(0,n).equals(str.substring(i,i+n))){
sep = str.substring(n,i+n).length();
return str.substring(0,n).equals(str.substring(sep, sep + n));
}
}
return false;
}
public boolean prefixAgain(String str, int n) {
ReplyDeleteint len = str.length();
String prefix = str.substring(0,n);
if(str.substring(n,len).contains(prefix))
return true;
return false;
}
public boolean prefixAgain(String str, int n) {
ReplyDeleteString number = str.substring(0,n);
String newStr = str.substring(n);
if(newStr.contains(number)){
return true;
} else {
return false;
}
}
public boolean prefixAgain(String str, int n) {
ReplyDeleteString checkString = str.substring(0, n);
String others=str.substring(n,str.length());
if (others.contains(checkString)) {
return true;
} else {
return false;
}
}
public boolean prefixAgain(String str, int n) {
ReplyDeleteif (n > str.length() / 2)
{
return false;
}
else
{
for (int i = str.length() - n; i > n - 1; i--)
{
if (str.substring(i, i + n).equals(str.substring(0, n)))
{
return true;
}
}
return false;
}
}
public boolean prefixAgain(String str, int n) {
ReplyDeleteString prefix = str.substring(0,n);
String nextPart = str.substring(n);
return nextPart.contains(prefix);
}
single line code:
ReplyDeletepublic boolean prefixAgain(String str, int n) {
return str.substring(str.indexOf(str.substring(0,n))+1)
.contains(str.substring(0,n));
}
public boolean xyzMiddle(String str) {
ReplyDeleteint countLeft = 0;
int countRight = 0;
int size = str.length();
String tar = "xyz";
String acc = "";
if(size < 3){
return false;
}
for(int i = 0; i < size - 2; i++){
acc = "";
// Traverse 3chars at a time, compare to xyz each step
acc = acc + str.charAt(i) + str.charAt(i + 1) + str.charAt(i + 2);
if(!acc.equals(tar)){
countLeft += 1;
}
else{
// Capture index where char z of xyz is found
int z = i + 2;
// Store count of remaining letters after xyz, size - 1 - found index z
countRight = size - 1 - z;
}
}
countLeft = countLeft - countRight;
int diff = countLeft - countRight;
if(diff == 1 || diff == 0 || diff == -1){
return true;
}
return false;
}
public boolean prefixAgain(String str, int n) {
ReplyDeleteString acc = "";
String comp = "";
for(int i = 0; i < n; i++){
acc = acc + str.charAt(i);
}
for(int i = n; i <= str.length() - n; i++){
comp = "";
for(int j = i; j < i + n; j++){
comp = comp + str.charAt(j);
}
if(acc.equals(comp)){
return true;
}
}
return false;
}
Another way
ReplyDeletepublic boolean prefixAgain(String str, int n) {
return (str.split(str.substring(0, n), - 1).length - 1) > 1;
}