-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathReversePrefixofWord.java
More file actions
38 lines (27 loc) · 881 Bytes
/
ReversePrefixofWord.java
File metadata and controls
38 lines (27 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Source: https://leetcode.com/problems/reverse-prefix-of-word/
Time: O(n), where n is the length of the given string(word)
Space: O(1), in-place, though we are using extra char array but we will still treat space as O(1) and not O(n)
because we usually don't consider the output in the space complexity
i.e. only temporary spaces which are used to get the desired output are considered.
*/
class Solution {
public String reversePrefix(String word, char ch) {
char[] c = word.toCharArray();
int len = c.length;
for(int i = 0; i < len; ++i) {
if(c[i] == ch) {
return reverseSegment(c, 0, i);
}
}
return word;
}
private String reverseSegment(char[] ch, int start, int end) {
while(start < end) {
char temp = ch[start];
ch[start++] = ch[end];
ch[end--] = temp;
}
return String.valueOf(ch);
}
}