给你一个下标从 0  开始的字符串 word 和一个字符 ch 。找出 ch 第一次出现的下标 i , 反转 word 中从下标 0 开始、直到下标 i 结束(含下标 i )的那段字符。如果 word 中不存在字符 ch ,则无需进行任何操作。
例如,如果 word = "abcdefd" 且 ch = "d" ,那么你应该 反转  从下标 0 开始、直到下标 3 结束(含下标 3 )。结果字符串将会是 " _ **dcba**_ efd" 。 
 
返回 结果字符串  。
示例 1: 
**输入:** word = " _ **abcd**_ efd", ch = "d"
**输出:** " _ **dcba**_ efd"
**解释:** "d" 第一次出现在下标 3 。 
反转从下标 0 到下标 3(含下标 3)的这段字符,结果字符串是 "dcbaefd" 。
 
示例 2: 
**输入:** word = " _ **xyxz**_ xe", ch = "z"
**输出:** " _ **zxyx**_ xe"
**解释:** "z" 第一次也是唯一一次出现是在下标 3 。
反转从下标 0 到下标 3(含下标 3)的这段字符,结果字符串是 "zxyxxe" 。
 
示例 3: 
**输入:** word = "abcd", ch = "z"
**输出:** "abcd"
**解释:** "z" 不存在于 word 中。
无需执行反转操作,结果字符串是 "abcd" 。
 
提示: 
1 <= word.length <= 250 
word 由小写英文字母组成 
ch 是一个小写英文字母 
 
方法一:直接反转 思路与算法 
首先查找 ch 在字符串 word 的位置,如果找到,则将字符串从下标 0 开始,到查找到的 ch 所在位置为止的那段字符串进行反转,否则直接返回原字符串。
代码 
[sol1-Python3] 1 2 3 4 class  Solution :    def  reversePrefix (self, word: str , ch: str  ) -> str :         i = word.find(ch) + 1          return  word[:i][::-1 ] + word[i:] 
 
[sol1-C++] 1 2 3 4 5 6 7 8 9 10 class  Solution  {public :    string reversePrefix (string word, char  ch)   {         int  index = word.find (ch);         if  (index != string::npos) {             reverse (word.begin (), word.begin () + index + 1 );         }         return  word;     } }; 
 
[sol1-Java] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class  Solution  {    public  String reversePrefix (String word, char  ch)  {         int  index  =  word.indexOf(ch);         if  (index >= 0 ) {             char [] arr = word.toCharArray();             int  left  =  0 , right = index;             while  (left < right) {                 char  temp  =  arr[left];                 arr[left] = arr[right];                 arr[right] = temp;                 left++;                 right--;             }             word = new  String (arr);         }         return  word;     } } 
 
[sol1-C#] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public  class  Solution  {    public  string  ReversePrefix (string  word, char  ch )  {         int  index = word.IndexOf(ch);         if  (index >= 0 ) {             char [] arr = word.ToCharArray();             int  left = 0 , right = index;             while  (left < right) {                 char  temp = arr[left];                 arr[left] = arr[right];                 arr[right] = temp;                 left++;                 right--;             }             word = new  string (arr);         }         return  word;     } } 
 
[sol1-C] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 char  * reversePrefix (char  * word, char  ch) {    char  *p2 = strchr (word, ch);     if  (p2 != NULL ) {         char  *p1 = word;         while  (p1 < p2) {             char  tmp = *p1;             *p1 = *p2;             *p2 = tmp;             p1++;             p2--;         }     }     return  word; } 
 
[sol1-Golang] 1 2 3 4 5 6 7 8 9 10 11 12 func  reversePrefix (word string , ch byte )   string  {    right := strings.IndexByte(word, ch)     if  right < 0  {         return  word     }     s := []byte (word)     for  left := 0 ; left < right; left++ {         s[left], s[right] = s[right], s[left]         right--     }     return  string (s) } 
 
[sol1-JavaScript] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var  reversePrefix = function (word, ch ) {    const  index = word.indexOf (ch);     if  (index >= 0 ) {         const  arr = [...word];         let  left = 0 , right = index;         while  (left < right) {             const  temp = arr[left];             arr[left] = arr[right];             arr[right] = temp;             left++;             right--;         }         word = arr.join ('' );     }     return  word; }; 
 
复杂度分析