Love Letter: Very simple code for Love Letter program of Nagarro's coding round question


Hello friends how are you all, Today in this blog i will teach you a very interesting program which is being asked today in coding round of a Multinational Software Company.  If you are preparing for job in a software company then this type of question will help you definitely. 

If you have only basic knowledge of programming still you can understand this program because i am giving a very simple code of this program.

Question: Love Letter

Description: You write a love letter to your friend. However , before your friend can read it, someone else takes it and rotates the characters of each words left to right K times. Find the number of words that remain the same even after this shifting of letters.

Input Specification: 

Input1: String of words                                                                                                                  Input2: K, number of times rotation happens

Output Specification:

Your function should return the number of correct words.


Example 1:
String:loHel endFri
K:3
Output:0
Expalation:Here "loHel endFri" is a rotated string so the original string was "Hello Friend" which is not correct. Hence the output will be 0.

Example 2:
String:Hello dFrien
K:5
Output:1
Expalation:Here "Hello dFrien" is a rotated string so the original string was "Hello Friend" so here one word Hello is remain same after rotation so the final output will be 1.

If you want to understand this program easily then you must have knowledge of the following topics:
 

Program: 
Here is the complete code of this program if you want to run this just copy the code and paste it into an editor and run it to check output.


class LoveLetter 
{
    public static int rotateWords(String words,int k)
    {
        //splitting the string into words 
        String rotateWord[]=words.split("\\s");
        //making a copy of original string
        String cpy=words.intern();
        //initializing count ot 0
        int count=0;
       //this loop is for total number of rotation        
       for(int i=1;i<=k;i++)
       {   //using this loop for ratotaion of character in each word
           for (int j = 0; j < rotateWord.length; j++) 
           {
              //rotating character of each word
              rotateWord[j]= rotateWord[j].substring(1)+rotateWord[j].charAt(0);
           }
       }
       //splitting the copy of string
       String actualWrod[]=cpy.split("\\s");
       
       //using this loop for rotating word
        for (int i = 0; i < rotateWord.length; i++) 
        {
            //using this loop for actual word
            for (int j = 0; j < actualWrod.length; j++) 
            {
               //comparing rotated words with actual words
               if(rotateWord[i].equals(actualWrod[j]))
                //increment the counter if found same word after rotation
                   count++;
            }
        }
        //return counter
        return count;
    }
    public static void main(String[] args) {
        //printing the results;
        System.out.println(rotateWords("adaada", 3));//1
        System.out.println(rotateWords("loHel endFri", 3));//0
        System.out.println(rotateWords("Hello dFrien", 5));//1
    }
    
}
/*
OUTPUT
1
0
1      
*/


If you wan this program in any other language then  just comment name of programming language i will give you the code in your specified language.

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks.

Don't use my blog post without my permission.

Post a Comment

10 Comments