Repeated String

Sort by

recency

|

14 Discussions

|

  • + 0 comments

    This is my solution

     // Complete the repeatedString function below.
    
    static long repeatedString(String s, long n) {
    long len= Long.valueOf(s.length());
        int a=0;
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == 'a') 
                ++a;
        }
         long occ = (n/len) * a;
        for (int i = 0; i < n%len; i++) {
            if (s.charAt(i) == 'a')
                ++occ;
        }
        return occ;
    }
    
  • + 0 comments

    This is my solution

    package hackerrankcode;

    import java.io.; import java.math.BigInteger; import java.util.;

    public class Repeated_String {

    public static BigInteger repeat(String s,long n){
    
        BigInteger min = new BigInteger("9223372036854775808");
    
        BigInteger increment = new BigInteger("1");
    
        List<Character> list=new ArrayList<>();
    
        for(long i=0;i<s.length();i++){
    
            list.add(s.charAt((int)i));
    
        }
    
        //System.out.println("The list is: "+list);
    
        if(list.contains("a")){
    
           // System.out.println("output from here");
    
            BigInteger bigInteger=new BigInteger("0");
    
            return bigInteger;
    
        }else if(n>=list.size() && n<=Math.pow(10, 6)){
    
            int y=0;
    
            while(list.size()!=n){
    
                list.add(s.charAt(y));
    
                y++;
    
                if(y==s.length()){
    
                    y=0;
    
                }
    
            }
    
        }else{
    
        BigInteger a = min.add(increment);    
    
            BigInteger bigInteger=new BigInteger(String.valueOf(n));
    
          //  System.out.println("Output from else");
    
            return a;
    
        }
         int a=0;
    
         //System.out.println("The list2 is: "+list);
    
        for(Character i:list){
    
            if(i=='a'){
    
                a++;
    
            }
    
        }
    
        BigInteger bigInteger=new BigInteger(String.valueOf(a));
    
        return bigInteger;
    
    }
    
    public static void main(String[] args) {
    
    Scanner scanner=new Scanner(System.in);    
    
    String repeatedString;
    long n;
    
        System.out.println("Enter your string: ");
        repeatedString=scanner.nextLine();
    
        System.out.println("Enter your number: ");
        n=scanner.nextLong();
    
        System.out.println(""+String.valueOf(repeat(repeatedString,n)));
    
    }}
    
    }
    

    //}

  • + 2 comments

    this is my solution

    public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); long n = in.nextLong();

        long numOfChar = 0 ;
        int numOfA = 0;
        int stringLength = s.length();
    
        if(stringLength > 0){
           for(int i=0;i<stringLength;i++){
              if(s.charAt(i)=='a'){
                numOfA++;
              } 
           } 
    
           long division = n / stringLength;
           long mod = n % stringLength;
           numOfChar = division * numOfA;
    
           for(int i=0;i<mod;i++){
               if(s.charAt(i)=='a'){
                   numOfChar++;
               }
           }
    
        }
    
        System.out.println(numOfChar);
    }
    
  • + 0 comments

    import sys

    s = input().strip() n = int(input().strip())

    l = len(s) if(l==1 and s=='a'): print(n) else: c = s.count('a') c *= n//l s = s[:(n%l)] c+=s.count('a') print(c)

  • [deleted]
    + 0 comments

    !/bin/python3

    import sys

    s = input().strip() n = int(input().strip()) c=s.count('a') f=int(n/len(s)) r=n%len(s) print(c*f+s[0:r].count('a'))