Prefix match with other strings | Practice | GeeksforGeeks

Given an array of strings arr[] of size n and given s a string str and an integer k. The task is to find the count of strings in arr[] whose prefix of length k matches with the k length prefix of str.

Example 1:
Input:
n = 6
arr[] = {“abba”, “abbb”, “abbc”, “abbd”, 
“abaa”, “abca”}
str = “abbg”
k = 3
Output: 4 
Explanation:
“abba”, “abbb”, “abbc” and “abbd” are 
the matching strings.



Example 2:
Input:
n = 3
arr[] = {“geeks”, “geeksforgeeks”, “forgeeks”}
str = “geeks”
k = 2
Output: 2


Your Task:  
You don't need to read input or print anything. Your task is to complete the function klengthpref() which takes the array of strings arr[], its size n and an integer k, a string str as input parameters and returns the count of strings in arr[] whose prefix of length k matches with the k length prefix of str.
Expected Time Complexity: O(n*l) where l is the length of the longest word in arr[].
Expected Auxiliary Space: O(n*l) where l is the length of the longest word in arr[].
 
Constraints:
1 <= n <= 1000
1 <= |arr[i]| , |str| <= 1000
1 <= k <= 1000
arr[i], str must contain only lowercase english alphabets
 
0
2017cstarun57053 days ago
nodeJS
 
      return arr.map((i)=>{         return i.substring(0,k) === str.substring(0,k) ? 1 : 0      }).filter((j)=>{          return j === 1      }).length
0
artistdarkangel1 week ago
Java Simple Solution | No need to use Trie concept.
 
class Solution{   public int klengthpref(String[] arr, int n, int k, String str)   {       str=str.substring(0,k);       int count=0;       for(int i=0; i<n; i++)       {           if(arr[i].length()<k)           continue;           String temp = arr[i].substring(0,k);           if(temp.equals(str))           count++;       }       return count;   }}
0
rishavkumar44121 week ago
Why am i getting wrong ans on submitting ?
class node{   node link[]=new node[26];   int cntstartwith;         node(){       cntstartwith=0;              for(int i=0;i<26;i++){           link[i]=null;       }   }}
class Solution{      public void insert(node root,String key){      int index;      node temp=root;            for(int i=0;i<key.length();i++){          index=key.charAt(i)-'a';          if(temp.link[index]==null){              temp.link[index]=new node();              temp.cntstartwith++;          }          temp=temp.link[index];      }         }           public int klengthpref(String[] arr, int n, int k, String str)   {       // code here       node root=new node();       for(int i=0;i<n;i++){           insert(root,arr[i]);       }              int index;       node temp=root;              for(int i=0;i<k;i++){           index=str.charAt(i)-'a';           if(temp.link[index]==null){               return 0;           }else{               temp=temp.link[index];           }       }       return temp.cntstartwith;   }}
0
kshitijmagare193 weeks ago
Total Time Taken:
0.7/8.61
JAVA :
public int klengthpref(String[] arr, int n, int k, String str)   {       // code here       String s = "";       for(int i = 0; i<k;i++){           s+=str.charAt(i);       }       //System.out.println(s);       int count = 0;       boolean b;       for(int i = 0; i< n;i++){           b = arr[i].startsWith(s);           if(b == true){               count++;           }                  }       return count;   }
0
creator23031 month ago
Easy C++ Solution :)
 
int klengthpref(string arr[], int n, int k, string str){ 
        string pat  ;
        int t=k;
        for(int i=0 ;i<str.size();i++)
        {
            if(k==0)
            break;
            pat+=str[i] ;
            k-- ;
        }
      
        int cnt =0 ;
        k=t;
        for(int i=0 ;i<n;i++)
        {

            if(arr[i].substr(0, k) == pat)
            cnt++;
        }
        return cnt;
    }
0
himanshujain4572 months ago
Java Solution Using Trie:
class Solution{   class TrieNode{       int count=0;       TrieNode child[]=new TrieNode[26];       TrieNode()       {           count=0;           for(int i=0;i<26;i++)           {               child[i]=null;           }       }          }   TrieNode root=new TrieNode();   void BuildTrie(String[] arr, int n)   {       for(int i=0;i<n;i++)       {           String s=arr[i];           TrieNode temp=root;           for(int k=0;k<s.length();k++)           {               char ch=s.charAt(k);               if(temp.child[ch-'a']==null)               {                   temp.child[ch-'a']=new TrieNode();               }               temp=temp.child[ch-'a'];               temp.count++;           }       }   }   public int klengthpref(String[] arr, int n, int k, String str)   {       BuildTrie(arr,n);            TrieNode temp=root;       for(int i=0;i<k;i++)       {           char ch=str.charAt(i);           if(temp.child[ch-'a']==null)           {               return 0;           }           temp=temp.child[ch-'a'];       }       return temp.count;   }}
0
paurushbatish2 months ago
Easy Solution using Trie Data Structure
Total Time Taken:
0.2/2.2
#define f(i,a,b) for(int i = a;i<b;i++)
struct Node {
  int freq;
  Node* child[26];
  Node(){
      freq = 1;
      f(i,0,26)child[i] = NULL;
  }
};
class Solution{   
public:
    Node* t = new Node();
    void insert(string s, Node* t){
        f(i,0, s.size()){
            int index = s[i] - 'a';
            if(!t->child[index]){
                t->child[index] = new Node();
            }else{
                t->child[index]->freq++;
            }
            t = t->child[index];
        }
    }
    int getfreq(string s, Node*t){
        f(i,0,s.size()){
            int index = s[i] - 'a';
            if(!t->child[index])return 0;
            t = t->child[index];
        }
        return t->freq;
    }
    int klengthpref(string arr[], int n, int k, string str){    
        string s = str.substr(0, k);
        f(i,0,n)insert(arr[i],t);
        return getfreq(s,t);
    }
};

0
aadityapburujwale3 months ago
public int klengthpref(String[] arr, int n, int k, String str)
    {
        
        str = str.substring(0,k);
        
        int ans = 0;

            for(String s:arr){
                if(s.startsWith(str))
                    ans++;
            }
        
        return ans;
    }
0
hitentandon3 months ago
Java:
		int count = 0;
       	str=str.substring(0,k);
       	for(String s:arr)
            count += s.indexOf(str)==0?1:0;
        return count;
0
abhishek11103 months ago
Java Solution:
public int klengthpref(String[] arr, int n, int k, String str)
    {
        str = str.substring(0, k);
        int count = 0;
        for (String s : arr) {
            if (k > s.length()) continue;
            String temp = s.substring(0, k);
            if (temp.equals(str)) {
                count++;
            }
        }
        return count;
    }
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.