Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

describe

Given a 0-indexed string word and a character ch, reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch (inclusive). If the character ch does not exist in word, do nothing.

  • For example, if word = “abcdefd” and ch = “d”, then you should reverse the segment that starts at 0 and ends at 3 (inclusive). The resulting string will be “dcbaefd”.

Return the resulting string.

Example 1:

Input: word = "abcdefd", ch = "d"
Output: "dcbaefd"
Explanation: The first occurrence of "d" is at index 3. 
Reverse the part of word from 0 to 3 (inclusive), the resulting string is "dcbaefd".
Copy the code

Example 2:

Input: word = "xyxzxe", ch = "z"
Output: "zxyxxe"
Explanation: The first and only occurrence of "z" is at index 3.
Reverse the part of word from 0 to 3 (inclusive), the resulting string is "zxyxxe".
Copy the code

Example 3:

Input: word = "abcd", ch = "z"
Output: "abcd"
Explanation: "z" does not exist in word.
You should not do any reverse operation, the resulting string is "abcd".
Copy the code

Note:

1 <= word.length <= 250
word consists of lowercase English letters.
ch is a lowercase English letter.
Copy the code

parsing

If ch is present in Word, then the string is reversed from the beginning to the substring at the position of CH. If ch is not present in Word, then the string is returned. So don’t do anything. The idea is simple:

  • If ch is not in Word, return word directly
  • Word [: I +1][::-1] + word[I +1:

answer

class Solution(object):
    def reversePrefix(self, word, ch):
        """
        :type word: str
        :type ch: str
        :rtype: str
        """
        if ch not in word:
            return word
        i = word.index(ch)
        return word[:i+1][::-1] + word[i+1:]
        	      
		
Copy the code

The results

Given in the Python online submissions. Memory Usage: 10000 ms Submissions in Python online submissions for Reverse Prefix of Word.Copy the code

parsing

In fact, the above method is to use built-in function, direct traversal is also ok, directly traversal word, each time to add a character to the beginning of result, until it finds ch, and then add the current CH to the first result. Finally, append the rest to the result result. This method is a bit of a fudge, anyway the problem is very simple, the code waves freely on the line.

answer

class Solution(object): def reversePrefix(self, word, ch): """ :type word: str :type ch: str :rtype: str """ if ch not in word: return word result = '' i = 0 for c in word: if c ! = ch: result = c + result i += 1 else: break result = word[i] + result return result + word[i+1:]Copy the code

The results

Given in the Python online submissions. Memory Usage: 10000 ms Given in the Python online submissions for Reverse Prefix of Word.Copy the code

Original link: leetcode.com/problems/re…

Your support is my biggest motivation