{"id":2782,"date":"2018-04-29T03:53:02","date_gmt":"2018-04-29T10:53:02","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2782"},"modified":"2018-04-30T00:32:16","modified_gmt":"2018-04-30T07:32:16","slug":"leetcode-824-goat-latin","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-824-goat-latin\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 824. Goat Latin"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>A sentence\u00a0<code>S<\/code>\u00a0is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.<\/p>\n<p>We would like to convert the sentence to &#8220;<em>Goat Latin&#8221;<\/em>\u00a0(a made-up language similar to Pig Latin.)<\/p>\n<p>The rules of Goat Latin are as follows:<\/p>\n<ul>\n<li>If a word begins with a vowel (a, e, i, o, or u), append\u00a0<code>\"ma\"<\/code>\u00a0to the end of the word.<br \/>\nFor example, the word &#8216;apple&#8217; becomes &#8216;applema&#8217;.<\/li>\n<li>If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add\u00a0<code>\"ma\"<\/code>.<br \/>\nFor example, the word\u00a0<code>\"goat\"<\/code>\u00a0becomes\u00a0<code>\"oatgma\"<\/code>.<\/li>\n<li>Add one letter\u00a0<code>'a'<\/code>\u00a0to the end of each word per its word index in the sentence, starting with 1.<br \/>\nFor example,\u00a0the first word gets\u00a0<code>\"a\"<\/code>\u00a0added to the end, the second word gets\u00a0<code>\"aa\"<\/code>\u00a0added to the end and so on.<\/li>\n<\/ul>\n<p>Return the\u00a0final sentence representing the conversion from\u00a0<code>S<\/code>\u00a0to Goat\u00a0Latin.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>\"I speak Goat Latin\"\r\n<strong>Output: <\/strong>\"Imaa peaksmaaa oatGmaaaa atinLmaaaaa\"\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>\"The quick brown fox jumped over the lazy dog\"\r\n<strong>Output: <\/strong>\"heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa\"\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Notes:<\/p>\n<ul>\n<li><code>S<\/code>\u00a0contains only uppercase, lowercase and spaces.\u00a0Exactly one space between each word.<\/li>\n<li><code>1 &lt;= S.length &lt;= 100<\/code>.<\/li>\n<\/ul>\n<h1><strong>Solution: String<\/strong><\/h1>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  string toGoatLatin(string S) {    \r\n    const string vowls = \"aeiouAEIOU\";\r\n    string ans;\r\n    string word;\r\n    int index = 0;\r\n    istringstream s(S);\r\n    while (s &gt;&gt; word) {\r\n      if (vowls.find(word[0]) == string::npos)          \r\n        word = word.substr(1) + word[0];      \r\n      ans += \" \" + word + \"ma\" + string(++index, 'a');\r\n    }\r\n    return ans.substr(1);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem A sentence\u00a0S\u00a0is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. We would like to convert the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,4],"class_list":["post-2782","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2782"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2782\/revisions"}],"predecessor-version":[{"id":2784,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2782\/revisions\/2784"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}