简体   繁体   中英

Program to find the number of 'a' in a string that is repeated n characters?

I am writing a program to find the number of 'a' in a given string that is repeated. For example, the call findAmountA("aba", 7) means that it finds the number of 'a' in the string "aba" repeated for 7 characters. So "abaabaa" is the final string, so that call would return 5.

Without actually making the string 7 characters (so calls for 1,000,000 characters would not take so long), how would I use mathematics to accomplish this task? I cannot get further than this, as I have been trying to troubleshoot this for a while.

Keep in mind I am a beginner Java programmer (Student) and do not want to use any advanced/fancy syntax that I would not learn in high school. Thank you!

public class AInString {
    public static void main(String[] args) {
        boolean a = findAmountA("aba", 10) == 7;
        boolean b = findAmountA("a", 100) == 100;
        boolean c = findAmountA("abca", 10) == 5;
        boolean d = findAmountA("", 10) == 0;
        boolean e = findAmountA("abcaa", 1000000) == 600000;
        boolean f = findAmountA("abc", 0) == 0;
        boolean g = findAmountA("bcd", 10) == 0;
        System.out.println(a && b && c && d && e && f && g);
    }
    public static int findAmountA(String word, int n) {
        String s = word;
        if(s.length() == 0 || aInWord(word) == 0) {
            return 0;
        }else {
            int a = (aInWord(s));
            return a;
        }
    }
    public static int aInWord(String word) {
        String s = word;
        int aInWord = 0;
        for(int i = 0; i < word.length(); i++) {
            if(s.charAt(i) == 'a') {
                aInWord++;
            }
        }
        return aInWord;
    }

}

Let's say your short string w has N copies of 'a' in it. Then the result string will consist of K copies of w followed by a possibility empty “tail” string.

The value of K can be determined by integer-dividing the number of 'a' s in the target string by N. Then the number t of 'a' s in the “tail” would be equal to the remainder of the division. Now you can print K copies of w followed by the shortest prefix of 'w' containing t 'a' s.

Now that you've counted the occurrences of a char a in a string word , you can count the occurrences of the char in the string extended n characters with:

return n / word.length() * aInWord(word) + aInWord(word.substring(0, n % word.length()));

n / word.length() gives the number of full repeats of the string that fit into n . Multiplying this by the count of aInWord(word) gives the count of a in repeats of word that fit cleanly into n .

The rest is a matter of finding the number of repeats in the substring of word that doesn't fit cleanly into n using the % modulus operator to find the size of the partial substring (if any). Adding the two counts together produces the total number of occurrences in the extended string.

Here is a clean version which avoids duplicate variables, extra conditionals and generalizes methods to maximize reusability:

class Main {
    public static void main(String[] args) {
        assert findAmount("aba", 10, "a") == 7;
        assert findAmount("a", 100, "a") == 100;
        assert findAmount("abca", 10, "a") == 5;
        assert findAmount("", 10, "a") == 0;
        assert findAmount("abcaa", 1000000, "a") == 600000;
        assert findAmount("abc", 0, "a") == 0;
        assert findAmount("bcd", 10, "a") == 0;
        System.out.println("tests passed");
    }

    public static int findAmount(String word, int n, String target) {
        if (word.length() == 0) {
            return 0;
        }

        return n / word.length() * count(target, word) + 
               count(target, word.substring(0, n % word.length()));
    }

    public static int count(String target, String s) {
        return s.length() - s.replace(target, "").length();
    }
}

Try it!

Divide the target length by the input length: for the example:

7 / 3 = 2 remainder 1

2 the number of "full copies" of the entire input string you will use. So, find the number of "a"s in the entire string, multiply by 2.

You will take the first 1 character of the input to make up the remainder of the 7 characters. Count the number of "a"s in that substring.

Simply add these two numbers together.

int total = count(input, "a") * targetLength / input.length()
          + count(input.substring(0, targetLength % input.length()), "a");

where count(input, c) is some method to count the number of occurrences of c in input .

I made some changes in your code, take a look:

public static void main(String[] args) {
    int a = findAmountA("aba", 10); // 7
    int b = findAmountA("a", 100); // 100;
    int c = findAmountA("abca", 10); //5;
    int d = findAmountA("", 10); //0;
    int f = findAmountA("abc", 0); //0;
    int g = findAmountA("bcd", 10); //0;
    System.out.println(a + " " + b + " " + c + " " + d + " " + f + " " + g);
}

public static int findAmountA(String word, int n) {
    if (word.length() < n) {
        for (int i=0; i<word.length(); i++) {
            while (word.length() < n) {
                word = word + word.charAt(i);
                break;
            }
        }
    } else if (word.length() > n) {
        for (int i=0; i<word.length(); i++) {
            word = word.substring(0, n);
        }
    } else {
        return aInWord(word);
    }
    return aInWord(word);
}

public static int aInWord(String word) {
    String s = word;
    int aInWord = 0;
    for(int i = 0; i < word.length(); i++) {
        if(s.charAt(i) == 'a') {
            aInWord++;
        }
    }

Thank you all for your help, using substrings I found an answer:

public class AInString {
    public static void main(String[] args) {
        boolean a = findAmountA("aba", 10) == 7;
        boolean b = findAmountA("a", 100) == 100;
        boolean c = findAmountA("abca", 10) == 5;
        boolean d = findAmountA("", 10) == 0;
        boolean e = findAmountA("abcaa", 1000000) == 600000;
        boolean f = findAmountA("abc", 0) == 0;
        boolean g = findAmountA("bcd", 10) == 0;
        System.out.println(a && b && c && d && e && f && g);
    }
    public static int findAmountA(String word, int n) {
        String s = word;
        if(s.length() == 0 || aInWord(s) == 0) {
            return 0;
        }else {
            int a = aInWord(s)*(n/s.length());
            int b = n % s.length();
            return a + aInWord(s.substring(0, b));
        }
    }
    public static int aInWord(String word) {
        String s = word;
        int aInWord = 0;
        for(int i = 0; i < word.length(); i++) {
            if(s.charAt(i) == 'a') {
                aInWord++;
            }
        }
        return aInWord;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM