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.
publicstaticlongrepeatedString(Strings,longn){// Write your code here/* A easy approach to be seen is: - Seperate problem into the number of 'a' in full string and in substring + Count 'a' in full string and multiply it with a number of duplication. + Count 'a' in substring which has (n % l) letters with l = s.length() Solution: 1. Calculate 'a' in substring, add it to variable "total" 2. Calculate 'a' in full string, then multiply with the number of duplication and add to "total" */longcount=0,total=0;intk=(int)(n%s.length());for(inti=0;i<k;i++){if(s.charAt(i)=='a')count++;}total+=count;//number of 'a' in the substringfor(inti=k;i<s.length();i++){if(s.charAt(i)=='a')count++;}//count now is the number of 'a' in the full stringlongduplication=n/s.length();total+=count*duplication;returntotal;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Repeated String
You are viewing a single comment's thread. Return to all comments →
My solution in Java15.