Find a string HackerRank Solution

This Python program is designed to solve a common problem in string manipulation: finding the number of occurrences of a specific substring within a larger string. The user inputs a string and a substring, and the program returns the count of the substring's occurrences in the string.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Find a string HackerRank Solution HackerRank solution using CodeRankGPT:

The solution works by iterating over the input string, checking a slice of the string of the same length as the substring against the substring itself. If they match, a counter is incremented. This process continues until the end of the string is reached, at which point the counter represents the total number of occurrences of the substring. This approach leverages Python's built-in string slicing and comparison capabilities.


def count_substring(string, sub_string):
    count_sub_string = 0
    for i in range(len(string) - len(sub_string) + 1):
        if string[i : i + len(sub_string)] == sub_string:
            count_sub_string += 1
    return count_sub_string

if __name__ == "__main__":
    string = input().strip()
    sub_string = input().strip()

    count = count_substring(string, sub_string)
    print(count)

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform