Split/Join is significantly less efficient than replace.
Always use curly braces on if statements for readability and maintainability.
toCharArray() creates a copy of the string. You should always avoid this method.
Do not use toCharArray(), it causes the string to be copied, where a copy is not necessary.
n >= arr.length
Doesn't work for all test cases.
Needs a test case where some words are a substring of previous words.
Example: "starborn star"
Fails for case "starborn star"
This comment is hidden because it contains spoiler information about the solution
Simple one liner using the Streams API
Class and method should be marked public. They have package level access by default.
One line version using streams. Still too many array locations, see my other solution for how to avoid array allocations.
This solution uses prefix recursion. Use tail recursion to collapse the stack depth.
How did this "solution" even get upvoted? Seems like somebody has been creating fake accounts to game the numbers.
The else case here doesn't read very well. The code would be more readable if the default case did not include a discount.