Distinct Substrings | Practice | GeeksforGeeks

Given a string s consisting of uppercase and lowercase alphabetic characters. Return the  number of distinct substrings of size 2 that appear in s as contiguous substrings.
Example
Input :
s = "ABCAB"
Output :
3
Explanation:  For "ABCAB", the 
three distinct substrings of size 
2 are "AB", "BC" and "CA". 
Example
Input :
s = "XYZ"
Output :
2
Explanation: For "XYZ", the 
two distinct substrings of size 2 are
"XY" and "YZ".

Your Task :
You don't need to read input or print anything. You have to complete the function fun() which takes the string s as input parameter and returns the number of distinct contiguous substring of size 2.
Expected Time Complexity : O(|s|)
Expected Auxilliary Space : O(|s|)
Constraints:
1<=|s|<=100
|s| denotes the length of the string s.
+1
sai8382633 weeks ago
class Solution {    int fun(String s)    {       // code here       Set<String> set=new HashSet<String>();       for(int i=0;i<s.length();i++)       {           for(int j=i;j<s.length();j++)           {               String str=s.substring(i,j+1);                              if(str.length()==2)               {                   set.add(str);                                  }           }       }       return set.size();   }} 
0
ankitparashxr2 months ago
java
Test Cases Passed:
127 / 127
Total Points Scored:
2/2
Total Time Taken:
0.1/1.2
Your Accuracy:
100%
 
Set<String> set = new HashSet<>();       for(int i=0;i<s.length();i++)       {           for(int j=i;j<s.length();j++)           {               String str = s.substring(i,j+1);               if(!set.contains(str) && str.length()==2)               {                   set.add(str);               }           }       }       return set.size();
+1
rathoredivya1502 months ago
int fun( string s)
{set<string>p;   for(int i=0;i<s.size()-1;i++){string t;  t+=s[i];  t+=s[i+1];  p.insert(t);}  int final=p.size();  return final;//code here}    
+1
sg6214283 months ago
class Solution 
{ 
    int fun(String s) 
    {
        int i=0;
        int count=0;
        String key="";
        Map<String,Integer>map=new HashMap<>();
        while((i+1)!=s.length())
        {
        key=s.substring(i,i+2);
        if(!map.containsKey(key))
        {
        map.put(key,i);
        count++;
        }
        i++;
        }
        return count;
    }
} 
+1
rajat25gupta3 months ago
Java Solution
 
int fun(String s)    {       // code here       HashSet<String> hs = new HashSet<>();       for(int i=0; i<s.length()-1; i++){           hs.add(s.substring(i, i+2));       }       return hs.size();   }
+1
abhiiishek074 months ago
int fun(string s){   //code here   unordered_set<string> st;   for(int i=0;i<s.size()-1;i++)   {       string str= s.substr(i,2);       st.insert(str);   }   return st.size();}
0
imranwahid6 months ago
Easy C++ solution
https://ide.geeksforgeeks.org/4YcCgitU6s
int fun(string s)
{
    unordered_set<string>dict;
    for(int i=0;i<s.length();i++)
    {
        if(i+1<s.length())
        {
            string tmp=s.substr(i,2);
            dict.insert(tmp);
        }
    }
    return dict.size();
}
+1
xahoor726 months ago
Chck this Out...
int fun(string s){   //code here   unordered_map<string,int>mp;   string st="";   int n=s.size();   for(int i=0;i<n;i++){       st+=s[i];       st+=s[i+1];       mp[st]++;       st="";   }   return mp.size()-1;      }
0
Jayesh Badgujar8 months ago
Jayesh Badgujar
https://uploads.disquscdn.c...
0
Dipanshu Tomar11 months ago
Dipanshu Tomar
Execution Time:0.26 https://uploads.disquscdn.c...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.