Sort by

recency

|

3724 Discussions

|

  • + 0 comments

    Time Complexity :

    def repeatedString(s:str, n:int):
        # Number of letter A in s
        n_of_a = s.count("a")
    
        #Lenght of s
        n_of_letters = len(s)
    
        # number of s based on n , rest of s if exist 
        number_of_substring , rest_of_substring = divmod(n,n_of_letters)
        
        # number of letter A in the rest of s
        rest = s[0:rest_of_substring].count("a")
        
        # so every substring there exist number of A Letter + rest if exist  
        result = (number_of_substring * n_of_a)  +  rest
    
        return result
    
  • + 0 comments

    Ruby solution:

    def repeatedString(s, n)
        indexes = []
        (0..s.length-1).each do |index|
           indexes << index if s[index] == 'a' 
        end
        
        return 0 if indexes.empty?
        
        count = 0
        size = s.size
        indexes.each do |index|
            count += ((n - 1 - index) / size) + 1
        end
        
        count
    end
    
  • + 0 comments
    def repeatedString(s, n):
        # Write your code here
        s_len = len(s)
        multiplier = n // s_len
        remaning = n % s_len
        a_count = 0
        remaning_a_count = 0
        for i, val in enumerate(s):
            if val == "a":
                a_count += 1
                if i < remaning:
                    remaning_a_count += 1
        a_count = a_count * multiplier
        a_count = a_count + remaning_a_count
        return a_count
    
  • + 0 comments

    Here's a solution in Python that does not need any for loops and can handle even high numbers for n


    def repeatedString(s: str, n: int) -> int:
        s_len = len(s)
        repetitions_needed = n // s_len
        remaining_chars = n % s_len
    
        occurrences_of_a = s.count('a') * repetitions_needed
    
        if remaining_chars:
            occurrences_of_a += s[:remaining_chars].count('a')
    
        return occurrences_of_a
    
  • + 0 comments
    public static long repeatedString(String s, long n) {
        // Write your code here
        long count=0;
          for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='a') count++;
          }
          count=count*(n/s.length());
          long rem=(n%(s.length()));
          for(int j=0;j<rem;j++){
             if(s.charAt(j)=='a') count++;
          }
         return count;
        }