Repeated String

  • + 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);
    }