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.
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 */
10 Comments
C++
ReplyDeletePlz reply as fast as u can
ReplyDeletePython
ReplyDeletepython
ReplyDeleteClick here to get code of this program in Python
DeletePlz provide code in C or in C++
ReplyDeletephp
ReplyDeleteThe second nested loop used for the actual words is not actually required...Please modify it so that others don't get confused.
ReplyDeletec++
ReplyDeleteThis comment has been removed by the author.
ReplyDelete