I'm posting about my solutions to the HackerRank challenges as a way to improve my learning, reinforce my knowledge, and establish an understanding of the concepts covered. If I help someone by sharing along the way, even better!
Repeated String is one of the initial challenges with 'EASY' difficulty.
Lilah has a string, s
, of lowercase English letters that are repeated infinitely. Given an integer, n
, find and return the number of letters a
in the first n
letters of Lilah's infinite string.
If Lilah's string is s = 'abcac'
and n = 10
, the substring we are considering is abcacabcac
, the first 10 characters of her infinite string. There are 4 occurrences of the letter a
in the substring.
A function that returns an integer number of occurrences of the letter a
in the prefix of length n
in the infinitely repeating string.
aba
10
7
function repeatedString (s , n ) {
let amount = parseInt (n / s .length ),
count = s .split ('' ).filter (c => c === 'a' ).length ,
rest = s .slice (0 , n % s .length ).split ('' ).filter (c => c === 'a' ).length ;
return amount * count + rest;
}
let amount = parseInt (n / s .length ),
count = s .split ('' ).filter (c => c === 'a' ).length ,
rest = s .slice (0 , n % s .length ).split ('' ).filter (c => c === 'a' ).length ;
All logic is contained in these three variables:
amount
is the amount of times the string s
appears based on n
of Lilah's infinite string;
count
is the total amount of a
contained in the string s
rest
is the total amount of a
contained in the string resulting from the remainder of dividing the given length n
with the string s
Finally, I finalize the function by returning the final count.
Other languages that I speak
def repeatedString (s , n )
amount = n / s .size
count = s .count ('a' )
rest = s .slice (0 , n % s .size ).count ('a' )
amount * count + rest
end
def repeatedString (s , n ):
amount = n // len (s )
count = s .count ('a' )
rest = s .count ('a' , 0 , n % len (s ))
return amount * count + rest
func repeatedString (s string , n int64 ) int64 {
var rest, amount, count int64
rest = n % int64 (len (s ))
amount = n / int64 (len (s ))
count = int64 (strings .Count (s , "a" ))
total := amount * count + int64 (strings .Count (s [0 :rest], "a" ))
return total
}
def repeatedString (s : String , n : Long ): Long = {
val rest = n % s .length
val amount = n / s .length
val count = s .count (_ == 'a' )
val total = amount * count + s .substring (0 , rest.toInt ).count (_ == 'a' )
return total
}
static int repeatedString (String s , long n ) {
long rest = n % s .length ();
long amount = n / s .length ();
long count = s.chars ().filter (c -> c == 'a' ).count ();
long total = amount * count + s .substring (0 , (int ) rest).chars ().filter (c -> c == 'a' ).count ();
return total;
}
function repeatedString ($s , $n ) {
$rest = substr_count (substr ($s , 0 , $n % strlen ($s )), "a" );
$amount = intval ($n / strlen ($s ));
$count = substr_count ($s , "a" );
return $amount * $count + $rest;
}
Thought: Life is a play that does not allow testing. So, sing, cry, dance, laugh and live intensely, before the curtain closes and the piece ends with no applause.