Tuesday, May 28, 2019

Coding challenge - Repeated string

When I popped out for some food today it dawned on me that there are basically two type of tests. First try to find out if you're good at drilling and memorising, and second one is looking for an ability to create your own solution. Sadly enough, first type claims to the the second type, but never vice versa.

My version of code is below and original task can be found here: LINK



package com.andrjushina.projects.codingchallenges;

public class RepeatedString {

    private static String s = "abac";
    private static long n = 1000L;
    private static char symbolToFind = 'a';
   
    public static void main(String[] args) {

        long symbolCount = repeatedString(s, n);
        System.out.println("Count of symbol: " + symbolCount);
      
    }
   
    static long repeatedString(String s, long n) {

        long entriesTotal = 0;
        long sLength = s.length();
        long stringsPerN = n/sLength;
        long symbolsPerNRemainder = n%s.length();
        long symbolEntriesPerString = 0;
        long symbolEntriesPerRemainder = 0;
       
        // calculating qty of symbols in the string
        for(int i=0; i<s.length(); i++){
            if(s.charAt(i)==symbolToFind){
                symbolEntriesPerString++;
            }
        }
       
        String sSubstring = s.substring(0,(int)symbolsPerNRemainder);

        for(int i=0; i<sSubstring.length(); i++){
            if(s.charAt(i)==symbolToFind){
                symbolEntriesPerRemainder++;
            }
        }
       
        if (symbolsPerNRemainder!=0) {
            entriesTotal = (int)symbolEntriesPerRemainder;
        }
             
        entriesTotal = (entriesTotal + symbolEntriesPerString*stringsPerN);
      
        return entriesTotal;

    }

}

No comments:

Post a Comment