VOOZH about

URL: https://www.geeksforgeeks.org/dsa/encoding-a-sentence-into-pig-latin/

⇱ Encoding a sentence into Pig Latin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Encoding a sentence into Pig Latin

Last Updated : 24 Aug, 2022

Design a program to take a sentence as an input, and then encode it into Pig Latin. 
 

A Pig Latin is an encrypted word in English, generated by placing the first letter of each word at the end, and then adding "ay" to the end.

Examples:

Input: s = "nevermind youve got them"
Output: "evermindnay ouveyay otgay hemtay"

Input: s = "sally knows best"
Output: "allysay nowskay estbay"

Approach: The problem can be solved by traversing the sentence and making the changes word by word, as follows:

  1. Find the index of the first letter of the word.
  2. Create a Pig Latin by appending the following these steps:
    • Append the first letter at the end of the substring of the word.
    • Add "ay" after appending the first letter.
  3. Continue the above steps till the last word in the string.

Below is the java program to implement the approach:


Output: 
allysay nowskay estbay

 

Time Complexity: O(N), where N is the length of sentence 
Auxiliary Space: O(N)

Comment