Problem Link: https://leetcode.com/problems/reverse-prefix-of-word/description/
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"
andch = "d"
, then you should reverse the segment that starts at0
and ends at3
(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".
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".
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".
Constraints:
1 <= word.length <= 250
word
consists of lowercase English letters.ch
is a lowercase English letter.
Explanation:
Method 1:
Given string s= “abcdefd” and character ch = “d”
Step 1:
First find out the “ch” in given string “s”.
For finding character in a string there is one method in the java is indexOf(). The method return the index of the character.
If character not found in the string then it return -1.
Step 2:
Take StringBuilder class
append the string start from 0 to indexOf(ch)
Example: append(s, 0, indexOf(ch)+1);
Here It append the string from 0 to end-1.
Step 3:
Reverse the string stored in the StringBuilder object.
Step 4:
After reversing the string the remaining part of the original string is added.
start from indexOf(ch)+1 to String.length();
Step 5:
Finally print the string.
CODE:
public class ReversePrefixWord {
public static void main(String[] args) {
String s = "abcdefxy";
char ch = 'd';
System.out.println("Original String: "+s);
System.out.println("After reversing prefix: "+reversePrefix(s,ch));
}
public static String reversePrefix(String word, char ch) {
int i = word.indexOf(ch);
StringBuilder sb = new StringBuilder();
// Here length of the stirng length is 0 or below then return the original string
if(i < 0){
return word;
}
//appending the array from 0 to index of character in string.
else{
sb.append(word, 0, i+1);
}
//reverse the string
sb.reverse();
//then append the index+1 to string length.
sb.append(word,i+1, word.length());
return sb.toString();
}
}
Original String: abcdefxy
After reversing prefix: dcbaefxy
Solution Link: https://leetcode.com/problems/reverse-prefix-of-word/submissions/1057005747/
Method 2:
Step 1:
First find out the “ch” in given string “s”.
For finding character in a string there is one method in the java is indexOf(). The method return the index of the character.
If character not found in the string then it return -1.
Step 2:
create a StringBuilder object and append the given string to the object.
Starting index left =0
Step 3:
swap the left ang right characters in the specified string left at 0 and right at s.charAt(ch);
In between the left and right swap the characters.
Here I used a method “setCharAt(index,character)”
It will take index and character. At mentioned index place the given character. swap the left and right characters
Step 4:
Print the final reversed prefix string.
CODE:
public class ReversePrefixWord {
public static void main(String[] args) {
String s = "abcdefxy";
char ch = 'd';
// System.out.println(s.indexOf(ch));
System.out.println("Original String: "+s);
System.out.println("After reversing prefix: "+reversePrefix(s,ch));
}
public static String reversePrefix(String s,char ch){
//step 1
int right = s.indexOf(ch);
//step 2
StringBuilder sb = new StringBuilder(s);
int left=0;
//step 3
while(left<right){
sb.setCharAt(left, s.charAt(right));
sb.setCharAt(right, s.charAt(left));
left++;
right--;
}
return sb.toString();
}
}
output:
Original String: abcdefxy
After reversing prefix: dcbaefxy
Solution Link: https://leetcode.com/problems/reverse-prefix-of-word/submissions/1106648987/
Method 3:
Step 1:
First find out the “ch” in given string “s”.
For finding character in a string there is one method in the java is indexOf(). The method return the index of the character.
If character not found in the string then it return -1.
Step 2:
calling the reverse method.
You can prefer the below link for explanation on reverse of a string
https://sirishachallagiri.hashnode.dev/reverse-of-a-string
CODE:
class Solution {
public static void main(String[] args) {
String s = "abcdefxy";
char ch = 'd';
// System.out.println(s.indexOf(ch));
System.out.println("Original String: "+s);
System.out.println("After reversing prefix: "+reversePrefix(s,ch));
}
public String reversePrefix(String word, char ch) {
int x=word.indexOf(ch);
return reverse(word,x);
}
public static String reverse(String s,int k){
char[] c = s.toCharArray();
for(int i=0,j=k;i<k/2;i++,j--){
char temp=c[i];
c[i]=c[j];
c[j]=temp;
}
return new String(c);
}
}
Output:
Original String: abcdefxy
After reversing prefix: dcbaefxy
Solution Link: https://leetcode.com/problems/reverse-prefix-of-word/submissions/1106655046/
Thank you for reading.
Happy Coding :)