Problem Link
Solution Link
Explanation:
This is a simple string manipulation problem. At first, we count the number of a's in the given string and the number is saved in countA variable. We just subtract the number of non- a characters from the string length to find that out. Since we are taking an-length part from an infinite-repeating string, the n-long part will have several occurrences of string s. The variable chunk determines the number of occurrences. So chunk*countA represents the number of a's in chunk number of s strings. We may have a remainder string portion for which we used a separate for loop to count the number of a's there. Last we print out the result.
Runtime: O(n) We used a single loop.
Note: Here n is long type variable because of the constraint 1<=n<=10^12. In Java integer range is -2,147,483,648 to 2,147,483, 647 .And the range for long is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
so we have chosen a long type variable for n.
You can learn more about Java's variable type ranges from this article by cs-fundamentals.
Solution Link
Explanation:
This is a simple string manipulation problem. At first, we count the number of a's in the given string and the number is saved in countA variable. We just subtract the number of non- a characters from the string length to find that out. Since we are taking an-length part from an infinite-repeating string, the n-long part will have several occurrences of string s. The variable chunk determines the number of occurrences. So chunk*countA represents the number of a's in chunk number of s strings. We may have a remainder string portion for which we used a separate for loop to count the number of a's there. Last we print out the result.
Runtime: O(n) We used a single loop.
Note: Here n is long type variable because of the constraint 1<=n<=10^12. In Java integer range is -2,147,483,648 to 2,147,483, 647 .And the range for long is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
so we have chosen a long type variable for n.
You can learn more about Java's variable type ranges from this article by cs-fundamentals.
Thanks for sharing good article. There is good string related java interview question visit Top String Program
ReplyDelete