View Single Post
  #1 (permalink)  
Old 07-01-2008, 10:25 PM
colin.cruise colin.cruise is offline
Member
 
Join Date: Jul 2008
Posts: 3
colin.cruise is on a distinguished road
Reverse and Replace a String in Linear Time
It would be much appreciated if I could have some help with the following problem. I would like to write a Java function that has the following interface:
public String reverseAndReplace(String original, String toBeReplaced, String replacement) { .. }

This method would do two things:
1) Reverse the words in the string.
For example, given the string: "Wealth is the product of man's capacity to think." The method would transform this sentence to: "think. to capacity man's of product the is Wealth"
2) Replace a word in the String with another word.
For example:
public String reverseAndReplace("Wealth is the product of man's capacity to think.", "think", "act");
The method would return: "act. to capacity man's of product the is Wealth"

This method should run in linear time, and it cannot use not any of Java's built-in string manipulation functions nor any third-party packages (such as Apache's StringUtils and the String.replaceAll methods) in its implementation. For example, this is illegal:

public String reverseAndReplace(String original, String toBeReplaced, String replacement) {
return StringUtils.reverseDelimited(original.replaceAll(t oBeReplaced, replacement), ' ');
}

Any help would be much appreciated and thanks in advance.
Reply With Quote
Sponsored Links