Skip to content

Commit 896a320

Browse files
committed
Reverse String - Day 4
1 parent 3dc292a commit 896a320

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.kishan.java.leetcode.juneChallenges;
2+
3+
/*
4+
* Two pointer approach
5+
*
6+
* */
7+
8+
import java.util.Arrays;
9+
10+
public class ReverseString {
11+
public static void reverseString(char[] array) {
12+
int head = 0;
13+
int tail = array.length - 1;
14+
while (head < tail) {
15+
char temp = array[head];
16+
array[head++] = array[tail];
17+
array[tail--] = temp;
18+
}
19+
}
20+
21+
public static void main(String[] args) {
22+
char example1[] = {'h', 'e', 'l', 'l', 'o'};
23+
String result1 = "olleh";
24+
System.out.println("Test 1");
25+
System.out.println("Before: " + Arrays.toString(example1).replace("[", "").replace("]", "").replace(", ", ""));
26+
reverseString(example1);
27+
System.out.println("After: " + Arrays.toString(example1).replace("[", "").replace("]", "").replace(", ", ""));
28+
System.out.println("Result: " + result1.equals(Arrays.toString(example1).replace("[", "").replace("]", "").replace(", ", "")));
29+
30+
char example2[] = {'H', 'a', 'n', 'n', 'a', 'h'};
31+
String result2 = "hannaH";
32+
System.out.println("Test 2");
33+
System.out.println("Before: " + Arrays.toString(example2).replace("[", "").replace("]", "").replace(", ", ""));
34+
reverseString(example2);
35+
System.out.println("After: " + Arrays.toString(example2).replace("[", "").replace("]", "").replace(", ", ""));
36+
System.out.println("Result: " + result2.equals(Arrays.toString(example2).replace("[", "").replace("]", "").replace(", ", "")));
37+
}
38+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.kishan.scala.leetcode.juneChallenges
2+
3+
/*
4+
- Write a function that reverses a string. The input string is given as an array of characters char[].
5+
- Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
6+
- You may assume all the characters consist of printable ascii characters.
7+
*/
8+
9+
/*
10+
Examples:
11+
12+
Input: ["h","e","l","l","o"]
13+
Output: ["o","l","l","e","h"]
14+
15+
Input: ["H","a","n","n","a","h"]
16+
Output: ["h","a","n","n","a","H"]
17+
18+
*/
19+
20+
/*
21+
* Space complexity: O(1)
22+
* Time complexity: O(n/2)
23+
* */
24+
25+
import scala.util.control.Breaks
26+
27+
object reverseString {
28+
29+
def main(args: Array[String]): Unit = {
30+
var example1: Array[Char] = Array('h', 'e', 'l', 'l', 'o')
31+
var result1: String = "olleh"
32+
println("Test 1")
33+
println(s"Before: ${example1.mkString("")}")
34+
reverseString(example1)
35+
println(s"After: ${example1.mkString("")}")
36+
println(s"Result: ${result1 == example1.mkString("")}")
37+
38+
var example2: Array[Char] = Array('H', 'a', 'n', 'n', 'a', 'h')
39+
var result2: String = "hannaH"
40+
println("Test 2")
41+
println(s"Before: ${example2.mkString("")}")
42+
reverseString(example2)
43+
println(s"After: ${example2.mkString("")}")
44+
println(s"Result: ${result2 == example2.mkString("")}")
45+
46+
}
47+
48+
def reverseString(s: Array[Char]): Unit = {
49+
val length = s.length
50+
val breaks = new Breaks()
51+
breaks.breakable {
52+
for (i <- 0 until length) {
53+
val temp = s(i)
54+
s(i) = s(length - 1 - i)
55+
s(length - 1 - i) = temp
56+
if (i == length / 2 - 1) {
57+
breaks.break()
58+
}
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)