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
108 changes: 102 additions & 6 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,129 @@
public class StringManipulation implements StringManipulationInterface {

private String currString;
private int stringLength;

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

@Override
public void setString(String string) {
currString = string;
stringLength = currString.length();
}

@Override
public int count() {
return 0;

if(currString.length()==0)//empty string has no words
return 0;

int wordCount = 0;
// Count the number of spaces to determine the number of words
for (int i = 0; i < stringLength; i++) {
if (currString.charAt(i) == ' ') {
wordCount++;
}
}

// Add 1 to the word count to account for the last word
return wordCount + 1;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if (n <= 0) {
throw new IllegalArgumentException("n is less than or equal to 0");
}

if (n > stringLength) {
throw new IndexOutOfBoundsException("n is greater than the length of the string");
}

String newString = "";

// Iterate through each character in the string
for (int i = 1; i <= stringLength; i++) {
// Check if the current position is a multiple of n
if (i % n == 0) {
// If maintainSpacing is true, add a space character
if (maintainSpacing) {
newString += ' ';
}
} else {
// Otherwise, add the character at the current position to the new string
newString += currString.charAt(i - 1);
}
}

return newString;
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
if (startWord <= 0 || endWord <= 0 || startWord > endWord) {
throw new IllegalArgumentException("Invalid parameters");
}

if (count() < endWord) {
throw new IndexOutOfBoundsException("endWord is greater than the total number of words in the string");
}

String[] subStrings = new String[endWord - startWord + 1];
int k = 0;
String currWord = "";
int currWordIndex = 0;

// Iterate through each character in the string
for (int i = 0; i < stringLength; i++) {
if (currString.charAt(i) == ' ') {
currWordIndex++;

// Check if the current word index is within the specified range
if (currWordIndex >= startWord && currWordIndex <= endWord) {
subStrings[k++] = currWord;
}

currWord = "";
} else {
currWord += currString.charAt(i);
}
}

currWordIndex++;

// Check if the last word is within the specified range
if (currWordIndex >= startWord && currWordIndex <= endWord) {
subStrings[k++] = currWord;
}

return subStrings;
}

@Override
public String restoreString(int[] indices) {
return null;
}
if (stringLength != indices.length) {
throw new IllegalArgumentException("Size of indices mismatches the size of the current string");
}

String shuffledString = new String(currString);
char[] shuffledArray = shuffledString.toCharArray();

// Iterate through each index in the indices array
for (int i = 0; i < stringLength; i++) {
// Check if the index is valid
if (indices[i] < 0 || indices[i] >= stringLength) {
throw new IndexOutOfBoundsException("Invalid index " + indices[i] + " in the indices array");
}

// Restore the character at the corresponding index
shuffledArray[indices[i]] = currString.charAt(i);
}

shuffledString = new String(shuffledArray);

return shuffledString;
}
}
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