Skip to content

Commit 881043e

Browse files
authored
Merge pull request ashutosh97#88 from plaidshirtakos/master
Reverse a string in place and Rod cutting problems are added
2 parents ef7360d + 30a066a commit 881043e

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

Reverse string in place/question.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Write a program to reverse a String in place in Java, without using additional memory.
2+
You cannot use any library classes or methods like e.g. StringBuilder to solve this problem. This restriction is placed because StringBuilder and StringBuffer class define a reverse() method which can easily reverse the given String.

Reverse string in place/solution.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* Java Program to reverse a String in place,
6+
* without any additional buffer in Java.
7+
*
8+
* @author WINDOWS 8
9+
*
10+
*/
11+
public class StringReversal {
12+
13+
/**
14+
* Java method to reverse a String in place
15+
* @param str
16+
* @return reverse of String
17+
*/
18+
public static String reverse(String str) {
19+
if(str == null || str.isEmpty()){
20+
return str;
21+
}
22+
char[] characters = str.toCharArray();
23+
int i = 0;
24+
int j = characters.length - 1;
25+
while (i < j) {
26+
swap(characters, i, j);
27+
i++;
28+
j--;
29+
}
30+
return new String(characters);
31+
}
32+
33+
/**
34+
* Java method to swap two numbers in given array
35+
* @param str
36+
* @param i
37+
* @param j
38+
*/
39+
private static void swap(char[] str, int i, int j) {
40+
char temp = str[i];
41+
str[i] = str[j];
42+
str[j] = temp;
43+
}
44+
45+
@Test
46+
public void reverseEmptyString(){
47+
Assert.assertEquals("", reverse(""));
48+
}
49+
50+
@Test
51+
public void reverseString(){
52+
Assert.assertEquals("cba", reverse("abc"));
53+
}
54+
55+
@Test
56+
public void reverseNullString(){
57+
Assert.assertEquals(null, reverse(null));
58+
}
59+
60+
@Test
61+
public void reversePalindromeString(){
62+
Assert.assertEquals("aba", reverse("aba"));
63+
}
64+
65+
@Test
66+
public void reverseSameCharacterString(){
67+
Assert.assertEquals("aaa", reverse("aaa"));
68+
}
69+
70+
@Test
71+
public void reverseAnagramString(){
72+
Assert.assertEquals("mary", reverse("yram"));
73+
}
74+
}

Rod cutting problem/question.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Given a rod of length n, and an array that contains the prices of all the pieces smaller than n, determine the maximum profit you could obtain from cutting up the rod and selling its pieces.

Rod cutting problem/solution.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class rod_cutting
2+
{
3+
static int cut_rod(int price[], int n)
4+
{
5+
// Declaring a 2D array, T
6+
int T[][] = new int[n-1][n+1];
7+
8+
// Initializing the array to all zeros
9+
for(int i=0; i < n-1; i++)
10+
{
11+
for(int j=0; j < n+1; j++ )
12+
{
13+
T[i][j] = 0;
14+
}
15+
}
16+
17+
for(int i=0; i < n-1; i++)
18+
{
19+
for(int j=0; j < n+1; j++ )
20+
{
21+
// First column => 0 length of rod => 0 profit
22+
if(j == 0) {
23+
continue;
24+
}
25+
26+
// First row => T[i-1][j] doesn't exist so just pick the second value
27+
else if(i == 0) {
28+
T[i][j] = price[i] + T[i][j-i-1];
29+
}
30+
31+
// where j <= i => T[i][j-i-1] doesn't exist so just pick the first value
32+
else if(j-i-1 < 0) {
33+
T[i][j] = T[i-1][j];
34+
}
35+
36+
// using the whole expression
37+
else {
38+
T[i][j] = Math.max(T[i-1][j], (price[i] + T[i][j-i-1]));
39+
}
40+
}
41+
}
42+
return T[n-2][n];
43+
}
44+
45+
public static void main(String args[])
46+
{
47+
int price[] = new int[] {2,5,7,8};
48+
int n = 5;
49+
System.out.println("Maximum profit is " + cut_rod(price, n));
50+
}
51+
}

0 commit comments

Comments
 (0)