{"id":8731,"date":"2021-11-20T08:53:32","date_gmt":"2021-11-20T16:53:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8731"},"modified":"2021-11-20T08:53:44","modified_gmt":"2021-11-20T16:53:44","slug":"leetcode-2000-reverse-prefix-of-word","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2000-reverse-prefix-of-word\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2000. Reverse Prefix of Word"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;string&nbsp;<code>word<\/code>&nbsp;and a character&nbsp;<code>ch<\/code>,&nbsp;<strong>reverse<\/strong>&nbsp;the segment of&nbsp;<code>word<\/code>&nbsp;that starts at index&nbsp;<code>0<\/code>&nbsp;and ends at the index of the&nbsp;<strong>first occurrence<\/strong>&nbsp;of&nbsp;<code>ch<\/code>&nbsp;(<strong>inclusive<\/strong>). If the character&nbsp;<code>ch<\/code>&nbsp;does not exist in&nbsp;<code>word<\/code>, do nothing.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, if&nbsp;<code>word = \"abcdefd\"<\/code>&nbsp;and&nbsp;<code>ch = \"d\"<\/code>, then you should&nbsp;<strong>reverse<\/strong>&nbsp;the segment that starts at&nbsp;<code>0<\/code>&nbsp;and ends at&nbsp;<code>3<\/code>&nbsp;(<strong>inclusive<\/strong>). The resulting string will be&nbsp;<code>\"<u>dcba<\/u>efd\"<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the resulting string<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> word = \"abcdefd\", ch = \"d\"\n<strong>Output:<\/strong> \"dcbaefd\"\n<strong>Explanation:<\/strong>&nbsp;The first occurrence of \"d\" is at index 3. \nReverse the part of word from 0 to 3 (inclusive), the resulting string is \"dcbaefd\".\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> word = \"xyxzxe\", ch = \"z\"\n<strong>Output:<\/strong> \"zxyxxe\"\n<strong>Explanation:<\/strong>&nbsp;The first and only occurrence of \"z\" is at index 3.\nReverse the part of word from 0 to 3 (inclusive), the resulting string is \"zxyxxe\".\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> word = \"abcd\", ch = \"z\"\n<strong>Output:<\/strong> \"abcd\"\n<strong>Explanation:<\/strong>&nbsp;\"z\" does not exist in word.\nYou should not do any reverse operation, the resulting string is \"abcd\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= word.length &lt;= 250<\/code><\/li><li><code>word<\/code>&nbsp;consists of lowercase English letters.<\/li><li><code>ch<\/code>&nbsp;is a lowercase English letter.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) \/ O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string reversePrefix(string word, char ch) {\n    for (size_t i = 0; i < word.size(); ++i)\n      if (word[i] == ch) return \n        string(rbegin(word) + word.size() - i - 1, rend(word)) + word.substr(i + 1);\n    return word;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;0-indexed&nbsp;string&nbsp;word&nbsp;and a character&nbsp;ch,&nbsp;reverse&nbsp;the segment of&nbsp;word&nbsp;that starts at index&nbsp;0&nbsp;and ends at the index of the&nbsp;first occurrence&nbsp;of&nbsp;ch&nbsp;(inclusive). If the character&nbsp;ch&nbsp;does not exist in&nbsp;word, do nothing. For&#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-8731","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\/8731","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=8731"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8731\/revisions"}],"predecessor-version":[{"id":8733,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8731\/revisions\/8733"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}