---
# System prepended metadata

title: 2000. Reverse Prefix of Word
tags: [Leetcode, Easy]

---

# 2000. Reverse Prefix of Word
###### tags: `Leetcode` `Easy`
Link: https://leetcode.com/problems/reverse-prefix-of-word/
## 思路 $O(N)$ $O(N)$
首先找到这个字母第一次出现的位置，然后用stringbuilder建string即可
## Code
```java=
class Solution {
    public String reversePrefix(String word, char ch) {
        int find = -1;
        for(int i = 0;i < word.length();i++){
            if(ch == word.charAt(i)){
                find = i;
                break;
            }
        }
        if(find == -1) return word;
        StringBuilder sb = new StringBuilder();
        for(int i = find;i >= 0;i--){
            sb.append(word.charAt(i));
        }
        for(int i = find+1;i < word.length();i++){
            sb.append(word.charAt(i));
        }
        return sb.toString();
    }
}
```