We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Repeated String
  5. Discussions

Repeated String

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • jhpar25
    4 years ago+ 13 comments

    Java Solution

    static long count(String s, long n) {
        long numOfS = n/s.length();
        long rest = n % s.length();
    
        if(!s.contains("a")) return 0;
        return s.length()>n? aCounter(s, rest) 
                : numOfS*aCounter(s, s.length()) + aCounter(s, rest);
    }
    
        private static long aCounter(String s, long end) {
            int a=0;
            for (int i = 0; i < end; i++) {
                if (s.charAt(i) == 'a') a++;
            }
            return a;
    }
    
    57|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature