reggaemuffin
reggaemuffin

Reputation: 1198

count directly repeated substring occurence

I am trying to count the number of directly repeatings of a substring in a string.

String s = "abcabcdabc";
String t = "abc";
int count = 2;

EDIT: because some people are asking, i try to clarify this: there are 3 times t in s but i need the number of times t is repeated without any other character. that would result in 2, because the d in my example is not the starting character of t. ('d' != 'a').

Another example to clarify this:

String s = "fooabcabcdabc";
String t = "abc";
int count = 0;

I know how to count the number of occurrences in the string, i need it to be repeating from left to right without interruption!

Here is what i have so far, but i think i made a simple mistake in it...

public static int countRepeat(String s, String t){
    if(s.length() == 0 || t.length() == 0){
        return 0;
    }
    int count = 0;
    if(t.length() == 1){
        System.out.println(s+" | " + t);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != t.charAt(0)){
                return count;
            }
            count++;
        }
    }else{
        System.out.println(s+" | " + t);
        for (int i = 0; i < s.length(); i++) {
            int tchar = (i- (count*(t.length()-1)));
            System.out.println(i+ " | " + tchar);
            if (s.charAt(i) != t.charAt(tchar)){
                return count;
            }
            if(tchar >= t.length()-1){
                count++;
            }
        }
    }
    return count;
}

what am i doing wrong? And is there a better/faster way to do this?

Upvotes: 0

Views: 2362

Answers (4)

fge
fge

Reputation: 121712

Using indexOf() makes the code much easier:

public static int startingRepeats(final String haystack, final String needle)
{
    String s = haystack;
    final int len = needle.length();

    // Special case...
    if (len == 0)
        return 0;

    int count = 0;

    while (s.startsWith(needle)) {
        count++;
        s = s.subString(len);
    }

    return count;
}

Upvotes: 1

aalku
aalku

Reputation: 2878

This version does not allocate new objects (substrings, etc) and just look for the characters where they are supposed to be.

public static void main(String[] args) {
    System.out.println(countRepeat("abcabcabc", "abc")); //  3
    System.out.println(countRepeat("abcabcdabc", "abc")); // 2
    System.out.println(countRepeat("abcabcabcx", "abc")); // 3
    System.out.println(countRepeat("xabcabcabc", "abc")); // 0
}
public static int countRepeat(String s, String t){
    int n = 0; // Ocurrences
    for (int i = 0; i < s.length(); i ++) { // i is index in s
        int j = i % t.length(); // corresponding index in t
        boolean last = j == t.length() - 1; // this should be the last char in t
        if (s.charAt(i) == t.charAt(j)) { // Matches?
            if (last) { // Matches and it is the last
                n++;
            }
        } else { // Do not match. finished!
            break;
        }
    }
    return n;
}

Upvotes: 1

Ashot Karakhanyan
Ashot Karakhanyan

Reputation: 2830

Here is the another calculator:

String s = "abcabcdabc";
String t = "abc";

int index = 0;
int count = 0;

while ((index = s.indexOf(t, index)) != -1) {
    index += t.length();
    count++;
}

System.out.println("count = " + count);

Upvotes: 0

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

There exists a str.indexOf(substring,index) method in the String API.

In pseudocode this would mean something like this:

declare integer variable as index 
declare integer variable as count
while index <= (length of string - length of substring)
  index = indexOf substring from index
  if index >= 0
    increment count
  end if
end while

Upvotes: 2

Related Questions