Number of substrings of one string present in other
Suppose we are given a string s1, we need to the find total number of substring(including multiple occurrences of the same substring) of s1 which are present in string s2.
Examples:
Input : s1 = aab
s2 = aaaab
Output :6
Substrings of s1 are ["a", "a", "b", "aa",
"ab", "aab"]. These all are present in s2.
Hence, answer is 6.
Input :s1 = abcd
s2 = swalencud
Output :3
The idea is to consider all substrings of s1 and check if it present in s2.
C++
// CPP program to count number of substrings of s1 // present in s2. #include<iostream> #include<string> using namespace std; int countSubstrs(string s1, string s2) { int ans = 0; for (int i = 0; i < s1.length(); i++) { // s3 stores all substrings of s1 string s3; for (int j = i; j < s1.length(); j++) { s3 += s1[j]; // check the presence of s3 in s2 if (s2.find(s3) != string::npos) ans++; } } return ans; } // Driver code int main() { string s1 = "aab", s2 = "aaaab"; cout << countSubstrs(s1, s2); return 0; } |
chevron_right
filter_none
Java
// Java program to count number of // substrings of s1 present in s2. import java.util.*; class GFG { static int countSubstrs(String s1, String s2) { int ans = 0; for (int i = 0; i < s1.length(); i++) { // s3 stores all substrings of s1 String s3 = ""; char[] s4 = s1.toCharArray(); for (int j = i; j < s1.length(); j++) { s3 += s4[j]; // check the presence of s3 in s2 if (s2.indexOf(s3) != -1) ans++; } } return ans; } // Driver code public static void main(String[] args) { String s1 = "aab", s2 = "aaaab"; System.out.println(countSubstrs(s1, s2)); } } // This code is contributed by ChitraNayal |
chevron_right
filter_none
Python 3
# Python 3 program to count number of substrings of s1 # present in s2. # Function for counting no. of substring # of s1 present in s2 def countSubstrs(s1, s2) : ans = 0 for i in range(len(s1)) : s3 = "" # s3 stores all substrings of s1 for j in range(i, len(s1)) : s3 += s1[j] # check the presence of s3 in s2 if s2.find(s3) != -1 : ans += 1 return ans # Driver code if __name__ == "__main__" : s1 = "aab" s2 = "aaaab" # function calling print(countSubstrs(s1, s2)) # This code is contributed by ANKITRAI1 |
chevron_right
filter_none
C#
// C# program to count number of // substrings of s1 present in s2. using System; class GFG { static int countSubstrs(String s1, String s2) { int ans = 0; for (int i = 0; i < s1.Length; i++) { // s3 stores all substrings of s1 String s3 = ""; char[] s4 = s1.ToCharArray(); for (int j = i; j < s1.Length; j++) { s3 += s4[j]; // check the presence of s3 in s2 if (s2.IndexOf(s3) != -1) ans++; } } return ans; } // Driver code public static void Main(String[] args) { String s1 = "aab", s2 = "aaaab"; Console.WriteLine(countSubstrs(s1, s2)); } } // This code is contributed // by Kirti_Mangal |
chevron_right
filter_none
PHP
<?php // PHP program to count number of // substrings of s1 present in s2. function countSubstrs($s1, $s2) { $ans = 0; for ($i = 0; $i < strlen($s1); $i++) { // s3 stores all substrings of s1 $s3 = ""; for ($j = $i; $j < strlen($s1); $j++) { $s3 += $s1[$j]; // check the presence of s3 in s2 if (stripos($s2, $s3, 0) != -1) $ans++; } } return $ans; } // Driver code $s1 = "aab"; $s2 = "aaaab"; echo countSubstrs($s1, $s2); // This code is contributed // by ChitraNayal ?> |
chevron_right
filter_none
Output:
6
Recommended Posts:
- Number of substrings of a string
- Number of even substrings in a string of digits
- Sum of all substrings of a string representing a number | Set 1
- Number of substrings with odd decimal value in a binary string
- Number of substrings divisible by 6 in a string of integers
- Count the number of vowels occurring in all the substrings of given string
- Given a binary string, count number of substrings that start and end with 1.
- Rearrange the string to maximize the number of palindromic substrings
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the character in first string that is present at minimum index in second string
- Most frequent word in first String which is not present in second String
- Remove characters from the first string which are present in the second string
- Replace two substrings (of a string) with each other
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



