Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 98 additions & 9 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,122 @@
public class StringManipulation implements StringManipulationInterface {

private String string;

@Override
public String getString() {
return null;
return string;
}

@Override
public void setString(String string) {
this.string = string;
}

@Override
public int count() {
return 0;
if (string == null || string.isEmpty()) {
return 0;
}

String trimmedString = string.trim();
if (trimmedString.isEmpty()) {
return 0;
}

// Split the string by whitespace to get individual words
String[] words = trimmedString.split("\\s+");

return words.length;
}



@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
}
if (n <= 0) {
throw new IllegalArgumentException("Error: n is less than or equal to 0");
}
else if (string == null) {
throw new NullPointerException("Error: String has not been initialized");
}
else if (n > string.length()) {
throw new IndexOutOfBoundsException("Error: n exceeds string length");
}


StringBuilder result = new StringBuilder();
int count = 0;
for (int i = 0; i < string.length(); i++) {
char currentChar = string.charAt(i);
count++;
if (count % n == 0 && maintainSpacing) {
result.append(' ');
}
if (count % n != 0) {
result.append(currentChar);
}


}

return result.toString();
}





@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
if (string == null) {
throw new NullPointerException("Error: string is not yet initialized");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null Pointer Exception error isn't required for this method. I'm pretty sure this is actually supposed to be an Illegal Argument Exception so I would modify this as part of that exception.

}
String[] words = string.trim().split("\\s+");
if(startWord <= 0 || endWord <= 0) {
throw new IllegalArgumentException("Error: start index and end index is less than 1");
}

else if (startWord > endWord) {
throw new IndexOutOfBoundsException("Error: start index is larger than end");
}

else if (endWord > words.length) {
throw new IndexOutOfBoundsException("Error: end index is larger than the string length");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you would be able to combine this else if statement with the one from line 79. This will help make the code shorter and will combine the same type of exception.
Ex: else if (startWord > endWord || endWord > words.length) {
throw new IndexOutOfBoundsException();
}

}

int substringsCount = endWord - startWord + 1;
String[] substrings = new String[substringsCount];

int index = 0;
for (int i = startWord - 1; i < endWord; i++) {
substrings[index++] = words[i];
}

return substrings;
}

@Override
public String restoreString(int[] indices) {
return null;
}
if (string == null) {
throw new NullPointerException("Error: string is not yet initialized");
}


}
if (indices.length != string.length()) {
throw new IllegalArgumentException("Error: string length is not equal to indices length");
}
for (int i = 0; i < indices.length - 1; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This outer nested for loop seems redundant. I'm not sure why you need to run the for loop for indices.length - 1. I believe you can just combine the 2 for loops into for (int i = 0; i < indices.length; i++) and then just run the exception in your if statement for if indices[i]< 0 or indices[i]>= string length

for (int j = i + 1; j < indices.length; j++) {
if (indices[i] == indices[j]) {
throw new IllegalArgumentException("Error: some indexes are not unique");
}
}
}

char[] restored = new char[indices.length];
for (int i = 0; i < indices.length; i++) {

restored[i] = string.charAt(indices[i]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can run this code in the original for loop at line 108 instead of writing a whole new for loop for this portion.

}
return new String(restored);
}
}
6 changes: 3 additions & 3 deletions src/main/java/StringManipulationInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public interface StringManipulationInterface {
* The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
* Return the shuffled string.
* example:
* Input: string = "UnitTest", indices = [4,5,6,7,0,1,2,3]
* Input: string = "UnitTest", indices = [4,5,6,7,0,2,1,3]
* Output: "TestUnit"
* Explanation:
* indices: 4 5 6 7 0 1 2 3
* indices: 4 5 6 7 0 2 1 3
* String: U n i t T e s t
* Actions to Shuffle: Shift U to 4th position, n to 5th position, i to 6th position ......
* Output: T e s t U n i t
Expand All @@ -95,7 +95,7 @@ public interface StringManipulationInterface {
* indices length is the same as the string length.
*
* throws IllegalArgumentException if not s.length == indices.length == n
* throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]>= string length
* throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length
*
* @param indices is an integer array for shuffled string new indices positions
* the character at the ith position moves to indices[i] in the shuffled string.
Expand Down
Loading