Find the string in grid | Practice | GeeksforGeeks

Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down and 4 diagonal directions.
 
Example 1:
Input: grid = {{a,b,c},{d,r,f},{g,h,i}},
word = "abc"
Output: {{0,0}}
Explanation: From (0,0) one can find "abc"
in horizontally right direction.

Example 2:
Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}}
,word = "abe"
Output: {{0,0},{0,2},{1,0}}
Explanation: From (0,0) one can find "abe" in 
right-down diagonal. From (0,2) one can
find "abe" in left-down diagonal. From
(1,0) one can find "abe" in Horizontally right 
direction.

 
Your Task:
You don't need to read or print anyhting, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containg the positions from where the word originates in any direction. If there is no such position then returns an empty list.
Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions strating from the same coordinates, the list should contain the coordinates only once. 
 
Expected Time Complexity: O(n*m*k) where k is constant
Exected Space Complexity: O(1)
 
Constraints:
1 <= n <= m <= 100
1 <= |word| <= 10
0
abhishekwasu20016 days ago
what is wrong with this code help plz;
 
 
 
class Solution {public:   vector<vector<int>>v;   vector<vector<int>>searchWord(vector<vector<char>>grid, string word){    int idx=0;    int size=word.length();    for(int i=0;i<grid.size();i++)    {        for(int j=0;j<grid[0].size();j++)        {             idx=0;            if(grid[i][j]==word[idx])             {                               //  for right-horizontal direction               //cout<<"now here we getting grid[i][j]="<<grid[i][j]<<" and word[idx]="<<word[idx]<<endl;                 int temp_i=i,temp_j=j,temp_idx=idx;                 while(temp_idx<word.length() && temp_i>=0 && temp_i<grid.size() && temp_j>=0 && temp_j<grid[0].size()&&grid[temp_i][temp_j]==word[temp_idx])                 {               //   cout<<"here grid word="<<grid[temp_i][temp_j]<<" and character in string "<<word[temp_idx]<<endl;                     vector<int>temp_v;                     if(temp_idx==size-1)                      {                                                temp_v.push_back(i);                          temp_v.push_back(j);                          v.push_back(temp_v);                       //   cout<<"1 here we have pushed i="<<i<<" and j="<<j<<endl;                          break;                      }                      temp_idx++;                      temp_j+=1;                 }               //  for left-horizontal direction                 temp_i=i,temp_j=j,temp_idx=0;                 while(temp_idx<word.length()&& temp_i>=0 && temp_i<grid.size() && temp_j>=0 && temp_j<grid[0].size()&&grid[temp_i][temp_j]==word[temp_idx] )                 {                     vector<int>temp_v;                     if(temp_idx==size-1)                      {                          temp_v.push_back(i);                          temp_v.push_back(j);                          v.push_back(temp_v);                       //   cout<<"2 here we have pushed i="<<i<<" and j="<<j<<endl;                          break;                      }                      temp_idx++;                      temp_j-=1;                 }               //  for up-vertical dirction                temp_i=i,temp_j=j,temp_idx=0;                 while(temp_idx<word.length() && temp_i>=0 && temp_i<grid.size() && temp_j>=0 && temp_j<grid[0].size()&&grid[temp_i][temp_j]==word[temp_idx])                 {                     vector<int>temp_v;                     if(temp_idx==size-1)                      {                          temp_v.push_back(i);                          temp_v.push_back(j);                          v.push_back(temp_v);                       //   cout<<"3 here we have pushed i="<<i<<" and j="<<j<<endl;                          break;                      }                      temp_idx++;                      temp_i-=1;                 }               //  for down vertical dirction              temp_i=i,temp_j=j,temp_idx=0;                 while(temp_idx<word.length()  && temp_i>=0 && temp_i<grid.size() && temp_j>=0 && temp_j<grid[0].size() && grid[temp_i][temp_j]==word[temp_idx])                 {                     vector<int>temp_v;                     if(temp_idx==size-1)                      {                          temp_v.push_back(i);                          temp_v.push_back(j);                          v.push_back(temp_v);                       //   cout<<"4 here we have pushed i="<<i<<" and j="<<j<<endl;                          break;                      }                      temp_idx++;                      temp_i+=1;                 }                                // for digonally right-down                temp_i=i,temp_j=j,temp_idx=0;                 while( temp_idx<word.length() && temp_i>=0 && temp_i<grid.size() && temp_j>=0 && temp_j<grid[0].size() && grid[temp_i][temp_j]==word[temp_idx])                 {                     vector<int>temp_v;                     if(temp_idx==size-1)                      {                          temp_v.push_back(i);                          temp_v.push_back(j);                          v.push_back(temp_v);                       //   cout<<"5 here we have pushed i="<<i<<" and j="<<j<<endl;                          break;                      }                      temp_idx++;                      temp_i++;                      temp_j++;                 }               //  for diagonally left-up                temp_i=i,temp_j=j,temp_idx=0;                 while( temp_idx<word.length() && temp_i>=0 && temp_i<grid.size() && temp_j>=0 && temp_j<grid[0].size() && grid[temp_i][temp_j]==word[temp_idx])                 {                     vector<int>temp_v;                     if(temp_idx==size-1)                      {                          temp_v.push_back(i);                          temp_v.push_back(j);                          v.push_back(temp_v);                       //   cout<<"6 here we have pushed i="<<i<<" and j="<<j<<endl;                          break;                      }                      temp_idx++;                      temp_i--;                      temp_j--;                 }               //  for diagonally right-up                 temp_i=i,temp_j=j,temp_idx=0;                 while(temp_idx<word.length() && temp_i>=0 && temp_i<grid.size() && temp_j>=0 && temp_j<grid[0].size() && grid[temp_i][temp_j]==word[temp_idx] )                 {                     vector<int>temp_v;                     if(temp_idx==size-1)                      {                          temp_v.push_back(i);                          temp_v.push_back(j);                          v.push_back(temp_v);                       //   cout<<"7 here we have pushed i="<<i<<" and j="<<j<<endl;                          break;                      }                      temp_idx++;                      temp_i--;                      temp_j++;               }                              temp_i=i,temp_j=j,temp_idx=0;                 while(temp_idx<word.length()&&temp_i>=0 && temp_i<grid.size() && temp_j>=0 && temp_j<grid[0].size() && grid[temp_i][temp_j]==word[temp_idx] )                 {                     vector<int>temp_v;                     if(temp_idx==size-1)                      {                          temp_v.push_back(i);                          temp_v.push_back(j);                          v.push_back(temp_v);                       //   cout<<"8 here we have pushed i="<<i<<" and j="<<j<<endl;                          break;                      }                      temp_idx++;                      temp_i++;                      temp_j--;               }         }               }}      return v;}
0
bokonist1 week ago
0.06/7 C++ solution. Brute force+ Recursive approach
.
.
class Solution {
public:
    #define UP 0
    #define UP_LEFT 1
    #define LEFT 2
    #define DOWN_LEFT 3
    #define DOWN 4
    #define DOWN_RIGHT 5
    #define RIGHT 6
    #define UP_RIGHT 7
    bool searchHelper(vector<vector<char>> &grid, string &word, int pos, int i, int j, int direction, int oi, int oj)
    {
    	if (pos == word.length())
    		return true;
    	int n = grid.size();
    	int m = grid[0].size();
    	int len = word.length();
    	if (i >= n || j >= m)
    		return false;
    	if (direction == UP && oi + 1 < len)
    		return false;
    	if (direction == RIGHT && m - oj + 1 < len)
    		return false;
    	if (direction == LEFT && oj + 1 < len)
    		return false;
    	if (direction == DOWN && n - oi + 1 < len)
    		return false;
    	if (direction == UP_LEFT && (oi + 1 < len || oj + 1 < len))
    		return false;
    	if (direction == DOWN_LEFT && (n - oi + 1 < len || oj + 1 < len))
    		return false;
    	if (direction == DOWN_RIGHT && (n - oi + 1 < len || m - oj + 1 < len))
    		return false;
    	if (direction == UP_RIGHT && (oi + 1 < len || m - oj + 1 < len))
    		return false;
    
    	if (grid[i][j] != word[pos])
    		return false;
    	else
    	{
    		if (direction == UP)
    			return searchHelper(grid, word, pos + 1, i - 1, j, direction, oi, oj);
    		if (direction == RIGHT)
    			return searchHelper(grid, word, pos + 1, i, j + 1, direction, oi, oj);
    		if (direction == LEFT)
    			return searchHelper(grid, word, pos + 1, i, j - 1, direction, oi, oj);
    		if (direction == DOWN)
    			return searchHelper(grid, word, pos + 1, i + 1, j, direction, oi, oj);
    		if (direction == UP_LEFT)
    			return searchHelper(grid, word, pos + 1, i - 1, j - 1, direction, oi, oj);
    		if (direction == DOWN_LEFT)
    			return searchHelper(grid, word, pos + 1, i + 1, j - 1, direction, oi, oj);
    		if (direction == DOWN_RIGHT)
    			return searchHelper(grid, word, pos + 1, i + 1, j + 1, direction, oi, oj);
    		if (direction == UP_RIGHT)
    			return searchHelper(grid, word, pos + 1, i - 1, j + 1, direction, oi, oj);
    	}
    }
    vector<vector<int>> searchWord(vector<vector<char>> grid, string word)
    {
    	// Code here
    	int n = grid.size();
    	int m = grid[0].size();
    	vector<vector<int>> ans;
    	vector<int> temp;
    	for (int i = 0; i < n; i++)
    	{
    		for (int j = 0; j < m; j++)
    		{
    			if (searchHelper(grid, word, 0, i, j, UP, i, j) ||
    			    searchHelper(grid, word, 0, i, j, UP_LEFT, i, j) ||
    			    searchHelper(grid, word, 0, i, j, UP_RIGHT, i, j) ||
    			    searchHelper(grid, word, 0, i, j, LEFT, i, j) ||
    			    searchHelper(grid, word, 0, i, j, DOWN, i, j) ||
    			    searchHelper(grid, word, 0, i, j, DOWN_LEFT, i, j) ||
    			    searchHelper(grid, word, 0, i, j, DOWN_RIGHT, i, j) ||
    			    searchHelper(grid, word, 0, i, j, RIGHT, i, j))
    			{
    				temp.push_back(i);
    				temp.push_back(j);
    				ans.push_back(temp);
    				temp.clear();
    			}
    		}
    	}
    	return ans;
    }
};
0
2019kuec20342 weeks ago
int x[8] = { -1, -1, -1,  0, 0,  1, 1, 1 };
int y[8] = { -1,  0,  1, -1, 1, -1, 0, 1 };
bool internalSearch(string needle, int row,int col, vector<vector<char>>hay,int row_max, int col_max, int xx,vector<vector<int>>&output)
{
 
    if (row >= 0 && row <= row_max && col >= 0 &&col <= col_max && needle[xx] == hay[row][col])
    {
        bool temp=false;
        
        char match = needle[xx];
        xx += 1;
 
        hay[row][col] = '*';
 
        if (xx==needle.size())
        {
            temp=true;
            output.push_back({row,col});
        }
        else
        {
 
            // through Backtrack searching
            // in every directions
            for(int i=0;i<8;i++)
        {
            
            int r=row+x[i];
            int c=col+y[i];
            bool y1=internalSearch(needle,r,c,hay,row_max,col_max,xx,output);
            if(y1==true)
            {
                break;
            }
            
        }
        }
        hay[row][col] = match;
        return temp;
    }

}
 
// Function to search the string in 2d array

vector<vector<int>> helper1(string needle,vector<vector<char>>grid, int row_count,int col_count)
{
    int r, c;
    vector<vector<int>>output;
 
    for (r = 0; r < row_count; ++r)
    {
        for (c = 0; c < col_count; ++c)
        {
            internalSearch(needle, r, c, grid,row_count - 1,col_count - 1, 0,output);
        }
    }
    return output;
}
	vector<vector<int>>searchWord(vector<vector<char>>grid, string word){
	    // Code here
	    vector<vector<int>>output=helper1(word,grid,grid.size(),grid[0].size());
	    return output;
	}
 
//can anybody tell what is wrong in this code please ??
0
harshasatya20012 weeks ago
class Solution{
    int[] x = { -1, -1, -1, 0, 0, 1, 1, 1 };
    int[] y = { -1, 0, 1, -1, 1, -1, 0, 1 };
 
    public int[][] searchWord(char[][] grid, String word)
    {
        ArrayList<int[]> a=new ArrayList<>();
        int n=grid.length;
        int m=grid[0].length;
        boolean[][] v=new boolean[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                    for(int l=0;l<8;l++){
                          if(dfs(grid,word,0,i,j,n,m,l)){
                              a.add(new int[]{i,j});
                     }
                }
            }
        }
        int[][] o=new int[a.size()][2];
        for(int k=0;k<a.size();k++){
            o[k][0]=a.get(k)[0];
            o[k][1]=a.get(k)[1];
        }
        return o;
    }
    boolean dfs(char[][] g,String w,int in,int i,int j,int n,int m,int k){
        if(in==w.length()){
            return true;
        }
        if(i<0 || j<0 || j>=m || i>=n || g[i][j]!=w.charAt(in)){
            return false;
        }
        return dfs(g,w,in+1,i+x[k],j+y[k],n,m,k);
    }
}
can someone explain why this code is failing?
+3
startlord17272 months ago
0.3/25.5 Seconds solution
SIMPLE JAVA. 
No backtracking necessary. Easy to understand recursive solution.
class Solution
{
    int[] xDir = {-1, 0, 1, -1, 1, -1, 0, 1};
    int[] yDir = {-1, -1, -1, 0, 0, 1, 1, 1};
    public int[][] searchWord(char[][] grid, String word)
    {
        // Code here
        ArrayList<int[]> ans = new ArrayList<>();
        int m=grid.length, n=grid[0].length;
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(grid[i][j]==word.charAt(0)){
                    if(search(grid, word.toCharArray(), 1, i, j, 0, 0)) ans.add(new int[]{i, j});
                }
            }
        }
        int[][] out = new int[ans.size()][2]; 
        for(int i=0; i<ans.size(); i++){
            out[i][0] = ans.get(i)[0];
            out[i][1] = ans.get(i)[1];
        }
        return out;
    }
    public boolean search(char[][] grid, char[] word, int k, int i, int j, int direction, int m){
        if(i<0 || j<0 || i>=grid.length || j>=grid[0].length) return false;
        if(k==word.length && grid[i][j]==word[k-1]) return true;
        
        if(word[k-1]!=grid[i][j]) return false;
        boolean found = false;
        for(int pos=0; pos<8; pos++){
            if(direction==-1){
                found = found || search(grid, word, k+1, i+yDir[m], j+xDir[m], -1, m);
            }
            else{
                found = found || search(grid, word, k+1, i+yDir[pos], j+xDir[pos], -1, pos);
            }
        }
        return found;
    }
}
0
aloksinghbais023 months ago
C++ solution using DFS approach having time complexity as O(n*m*8) and space complexity as O(1) is as follows :- 
 
Execution Time :- 0.0 / 7.0 sec
 
vector<vector<int>> ans;   bool flag;      bool isValid(int x,int y,int n,int m){       if(x < 0 || x >= n || y < 0 || y >= m) return (false);       return (true);   }      void helper(vector<vector<char>> &grid,int x,int y,string word,int i,int sti,int stj,int dir){       if(i == word.length()-1){           ans.push_back({sti,stj});           flag = true;           return;       }              int dx[] = {-1,-1,0,1,1,1,0,-1};       int dy[] = {0,1,1,1,0,-1,-1,-1};              int new_x = x + dx[dir];       int new_y = y + dy[dir];              if(isValid(new_x,new_y,grid.size(),grid[0].size()) && grid[new_x][new_y] == word[i+1]){           helper(grid,new_x,new_y,word,i+1,sti,stj,dir);       }   }   vector<vector<int>> searchWord(vector<vector<char>>grid, string word){    int n = grid.size();    int m = grid[0].size();        for(int i=0; i<n; i++){        for(int j=0; j<m; j++){            if(grid[i][j] == word[0]){                   if(word.length() > 1){                       int dx[] = {-1,-1,0,1,1,1,0,-1};                       int dy[] = {0,1,1,1,0,-1,-1,-1};                                              for(int k=0; k<8; k++){                           int new_x = i + dx[k];                           int new_y = j + dy[k];                           if(isValid(new_x,new_y,n,m) && grid[new_x][new_y] == word[1]){                               flag = false;                               helper(grid,new_x,new_y,word,1,i,j,k);                               if(flag){                                   break;                               }                           }                       }                   }                   else{                       ans.push_back({i,j});                   }            }        }    }        return (ans);}
0
potdukheamit3 months ago
CAN ANYONE TELL WHATS WRONG IN THIS CODE ITS GIVING -1 FOR EVERY INPUT..
 
// { Driver Code Starts#include<bits/stdc++.h>using namespace std;
// } Driver Code Endsclass Solution {public:   bool isSafe(vector<vector<char>>&grid, string word, int row, int col, int idx) {       if(row<0 || col<0 || row>grid.size() || col<grid[0].size()) return false;              if(grid[row][col] == word[idx]) {           return true;       }       return false;   }      void solve(vector<vector<char>>&grid, string word, vector<vector<int>> &ans,        vector<int>& list, int idx,int row, int col) {              if(idx==word.length()) {           ans.push_back(list);           return;       }              grid[row][col] = '*';              if(isSafe(grid, word, row+1, col, idx+1)) {           list.push_back(row+1);           list.push_back(col);           solve(grid, word, ans, list, idx+1, row+1, col);       }       if(isSafe(grid, word, row-1, col, idx+1)) {           list.push_back(row-1);           list.push_back(col);           solve(grid, word, ans, list, idx+1, row-1, col);       }       if(isSafe(grid, word, row, col+1, idx+1)) {           list.push_back(row);           list.push_back(col+1);           solve(grid, word, ans, list, idx+1, row, col+1);       }       if(isSafe(grid, word, row, col-1, idx+1)) {           list.push_back(row);           list.push_back(col-1);           solve(grid, word, ans, list, idx+1, row, col-1);       }              grid[row][col] = word[idx];       return;   }   vector<vector<int>>searchWord(vector<vector<char>>grid, string word){    vector<vector<int>> ans;    vector<int> list;        for(int i=0;i<grid.size();i++) {        for(int j=0;j<grid[0].size();j++) {            if(grid[i][j] == word[0]) {                solve(grid, word, ans, list, 0, i, j);            }        }    }    return ans;}};
// { Driver Code Starts.int main(){int tc;cin >> tc;while(tc--){ int n, m; cin >> n >> m; vector<vector<char>>grid(n, vector<char>(m,'x')); for(int i = 0; i < n; i++){  for(int j = 0; j < m; j++)   cin >> grid[i][j]; } string word; cin >> word; Solution obj; vector<vector<int>>ans = obj.searchWord(grid, word); for(auto i: ans){  for(auto j: i)   cout << j << " ";  cout << "\n"; } if(ans.size() == 0) {     cout<<"-1\n"; }}return 0;}  // } Driver Code Ends
0
abhishekpanwar6973 months ago
bool find(int i,int j,int n,int m,vector<vector<char>>&a,int pos,string word,int x,int y){   if(pos==word.length())   return true;   if(i<0||j<0||i>=n||j>=m||a[i][j]!=word[pos])   return false;   return find(i+x,j+y,n,m,a,pos+1,word,x,y);}vector<vector<int>>searchWord(vector<vector<char>>a, string word){    vector<vector<int>>v;    int n=a.size(),m=a[0].size();    int x[8] = {1,-1,0,0,1,-1,1,-1};       int y[8] = {1,-1,1,-1,0,0,-1,1};       for(int i=0;i<n;i++)       {           for(int j=0;j<m;j++)           {               if(a[i][j]==word[0])               {                  for(int k=0;k<8;k++)                  {                      if(find(i,j,n,m,a,0,word,x[k],y[k]))                      {                      v.push_back({i,j});                      break;                      }                  }               }           }       }       return v;}
0
goelharsh493 months ago
JAVA SIMPLE AND EFFICIENT SOLUTION
// { Driver Code Starts
//Initial Template for Java

import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine().trim());
        while(T-->0)
        {
            String[] s1 = br.readLine().trim().split(" ");
            int n = Integer.parseInt(s1[0]);
            int m = Integer.parseInt(s1[1]);
            char[][] grid = new char[n][m];
            for(int i = 0; i < n; i++){
                String S = br.readLine().trim();
                for(int j = 0; j < m; j++){
                    grid[i][j] = S.charAt(j);
                }
            }
            String word = br.readLine().trim();
            Solution obj = new Solution();
            int[][] ans = obj.searchWord(grid, word);
            for(int i = 0; i < ans.length; i++){
                for(int j = 0; j < ans[i].length; j++){
                    System.out.print(ans[i][j] + " ");
                }
                System.out.println();
            }
            if(ans.length==0)
            {
                System.out.println("-1");
            }

        }
    }
}
// } Driver Code Ends


//User function Template for Java

class Solution
{
    public int[][] searchWord(char[][] grid, String word)
    {
        ArrayList<Pair> l = new ArrayList<>();
        
        for(int i=0;i<grid.length;i++)
            for(int j=0;j<grid[0].length;j++)
                for(int k=0;k<8;k++)
                    if(s(k,grid,word,i,j,0,grid.length,grid[0].length,word.length()))
                        l.add(new Pair(i,j));
                        
        int[][] a = new int[l.size()][2];
        
        for(int i=0;i<l.size();i++){
            a[i][0] = l.get(i).x; 
            a[i][1] = l.get(i).y;
        }
        
        return a;
    }
    
    public boolean s(int k,char[][] a,String w,int i,int j,int p,int n,int m,int s){
        if(p == s)
            return true;
            
        if(i<0 || j<0 || i==n || j==m)
            return false;
        
        if(w.charAt(p) == a[i][j]){
            switch(k){
                case 0:
                    return s(k,a,w,i+1,j,p+1,n,m,s) ;
                case 1:
                    return s(k,a,w,i,j+1,p+1,n,m,s);
                case 2:
                    return s(k,a,w,i-1,j,p+1,n,m,s);
                case 3:
                    return s(k,a,w,i,j-1,p+1,n,m,s);
                case 4:
                    return s(k,a,w,i+1,j+1,p+1,n,m,s) ;
                case 5:
                    return s(k,a,w,i-1,j+1,p+1,n,m,s) ;
                case 6:
                    return s(k,a,w,i+1,j-1,p+1,n,m,s) ;
                default :
                    return s(k,a,w,i-1,j-1,p+1,n,m,s);
            }
        }
        else
            return false;
    }
}

class Pair{
    int x,y;
    
    Pair(int x,int y){
        this.x = x;
        this.y = y;
    }
}
+1
kantariyaraj3 months ago
JAVA solution. Don't know why the test case is failing. Working perfect in other ide.
public int[][] searchWord(char[][] grid, String word)
   {
       int[] x = {0,-1,-1,-1,0,1,1,1};
       int[] y = {-1,-1,0,1,1,1,0,-1};
       
       int n = grid.length;
       int m = grid[0].length;
       
       ArrayList<Integer> listX = new ArrayList<>();
       ArrayList<Integer> listY = new ArrayList<>();
       
       for(int i = 0; i < n; i++){
           for(int j = 0; j < m; j++){
               if(grid[i][j] == word.charAt(0)){
                   for(int k = 0; k < 8; k++){
                       int s=i+x[k];
                       int e=j+y[k];
                       int count = 1;
                       while(s >= 0 && e >= 0 
                               && s < n && e < m 
                               && grid[s][e] == word.charAt(count)){
                           s+=x[k];
                           e+=y[k];
                           if(++count == word.length()) break;
                       } 
                       if(count == word.length()){
                           listX.add(i);
                           listY.add(j);
                       }
                   }
               }
           }
       }
       int r = listX.size();
       int[][] result = new int[r][2];
       for(int i = 0; i< r;i++){
           result[i][0] = listX.get(i);
           result[i][1] = listY.get(i);
       }
       
       return result;
   }
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.