Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathReverseString.java
More file actions
Latest commit
102 lines (95 loc) · 2.99 KB
/
Copy pathReverseString.java
File metadata and controls
102 lines (95 loc) · 2.99 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
packagecom.thealgorithms.strings;
importjava.util.Stack;
/**
* Reverse String using different version
*/
publicfinalclassReverseString {
privateReverseString() {
}
/**
* easiest way to reverses the string str and returns it
*
* @param str string to be reversed
* @return reversed string
*/
publicstaticStringreverse(Stringstr) {
returnnewStringBuilder(str).reverse().toString();
}
/**
* second way to reverses the string str and returns it
*
* @param str string to be reversed
* @return reversed string
*/
publicstaticStringreverse2(Stringstr) {
if (str == null || str.isEmpty()) {
returnstr;
}
char[] value = str.toCharArray();
for (inti = 0, j = str.length() - 1; i < j; i++, j--) {
chartemp = value[i];
value[i] = value[j];
value[j] = temp;
}
returnnewString(value);
}
/**
* Reverse version 3 the given string using a StringBuilder.
* This method converts the string to a character array,
* iterates through it in reverse order, and appends each character
* to a StringBuilder.
*
* @param string The input string to be reversed.
* @return The reversed string.
*/
publicstaticStringreverse3(Stringstring) {
if (string.isEmpty()) {
returnstring;
}
char[] chars = string.toCharArray();
StringBuildersb = newStringBuilder();
for (inti = string.length() - 1; i >= 0; i--) {
sb.append(chars[i]);
}
returnsb.toString();
}
/**
* Reverses the given string using a stack.
* This method uses a stack to reverse the characters of the string.
* @param str The input string to be reversed.
* @return The reversed string.
*/
publicstaticStringreverseStringUsingStack(Stringstr) {
// Check if the input string is null
if (str == null) {
thrownewIllegalArgumentException("Input string cannot be null");
}
Stack<Character> stack = newStack<>();
StringBuilderreversedString = newStringBuilder();
// Check if the input string is empty
if (str.isEmpty()) {
returnstr;
}
// Push each character of the string onto the stack
for (charch : str.toCharArray()) {
stack.push(ch);
}
// Pop each character from the stack and append to the StringBuilder
while (!stack.isEmpty()) {
reversedString.append(stack.pop());
}
returnreversedString.toString();
}
/**
* Reverse the String using Recursion
* @param str string to be reversed
* @return reversed string
*/
publicstaticStringreverseStringUsingRecursion(Stringstr) {
if (str.isEmpty()) {
returnstr;
} else {
returnreverseStringUsingRecursion(str.substring(1)) + str.charAt(0);
}
}
}