L
L
lover_loser2021-01-10 12:47:23
Java
lover_loser, 2021-01-10 12:47:23

Adding a string to a string up to a certain (infinitely large) number and finding the character 'a' in it. How to decide?

example
, given the string "aba" and the number 10
, we add to the string 7 characters from the string "aba" in turn, as a result, the string should be "abaabaabaa"
Count the number of characters 'a'
the method should return 7
// what I did does not pass all tests
example of failed test
string "udjlitpopjhipmwgvggazhuzvcmzhulowmveqyktlakdufzcefrxufssqdslyfuiahtzjjdeaxqeiarcjpponoclynbtraaawrps"
number 872514961806
required result 69801196944
\\ example of my code

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the repeatedString function below.
    static long repeatedString(String s, long n) {
        if (n > Integer.MAX_VALUE){

            return n;
        }
        long count = n - s.length();
        long repeatedLetter = 0;

        StringBuilder sBuilder = new StringBuilder(s);
        for (int i = 0; i < count; i++) {
            sBuilder.append(sBuilder.charAt(i));
        }
        s = sBuilder.toString();

        char[] sArray = s.toCharArray();
        for (char c : sArray) {
            if (c == 'a') {
                repeatedLetter++;
            }
        }
    return repeatedLetter;
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String s = scanner.nextLine();

        long n = scanner.nextLong();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        long result = repeatedString(s, n);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Erik Mironov, 2021-01-10
@lover_loser

You don't need to concatenate billions of characters, you can count the number of repetitions in the original string and then multiply that number by the number of complete repetitions of the string + the remainder

static long repeatedString(String s, long n) {
        /* Кол-во повторов в исходной строке */
        int rep = (int) s.toLowerCase()
                .chars()
                .filter(c -> c == 'a').count();

        /* Кол-во циклов, в которых исходная строка будет полностью скопирована */
        long cycles = (n / s.length()) - 1;

        /* Кол-во повторов в остаточной строке, которой не хватило на полный цикл */
        int remainder = (int) s.toLowerCase()
                .substring(0, (int) n % s.length())
                .chars()
                .filter(c -> c == 'a').count();

        return rep * cycles + remainder + rep;
}

C
cdracm, 2021-01-10
@cdracm

I will answer this question with a question. how much memory does it take to repeat the string "udjlitpopjhipmwgvggazhuzvcmzhulowmveqyktlakdufzcefrxufssqdslyfuiahtzjjdeaxqeiarcjpponoclynbtraaawrps" 872514961806 times?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question