0% found this document useful (0 votes)
20 views1 page

Repeated Sub String

This code checks how many times a substring repeats in a string that is repeated a given number of times. It takes a string, repeat count, and substring to check for as inputs, repeats the string and counts the occurrences of the substring, returning the count.

Uploaded by

sushant sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views1 page

Repeated Sub String

This code checks how many times a substring repeats in a string that is repeated a given number of times. It takes a string, repeat count, and substring to check for as inputs, repeats the string and counts the occurrences of the substring, returning the count.

Uploaded by

sushant sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

public class RepeatedSubString {

public static void main(String[] args) {

String str = "aba";

int repeatCount = 10;

String checkFor = "a";

long result = checkRepeatedCharCount(str, repeatCount, checkFor);

System.out.println(result);

private static Long checkRepeatedCharCount(String str, int repeatCount, String checkFor) {

int strLength = str.length();

StringBuilder sb = new StringBuilder(str);


int counter = 0;

for (int i = 0; i < repeatCount - strLength; i++) {

if (counter < strLength) {


sb.append(str.charAt(counter));
counter++;
}

if (counter > strLength - 1) {


counter = 0;
}

List<Character> list = new ArrayList<>();

for (char ch : sb.toString().toCharArray()) {

list.add(ch);
}

System.out.println(sb.toString());
Map<Object, Long> optional = list.stream().sorted().filter(n -> n.equals('a'))
.collect(Collectors.groupingBy(n -> n, Collectors.counting()));

System.out.println(optional);
long val = 0;
for (Map.Entry<Object, Long> entry : optional.entrySet()) {
// Object key = entry.getKey();
val = entry.getValue();
return val;
}

return val;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy