patternjavaMinor
Super Reduced String
Viewed 0 times
superstringreduced
Problem
Online challenge on Hacker Rank.
Problem Statement
Steve has a string,
Steve wants to reduce as much as possible. To do this, he will repeat the >above operation as many times as it can be performed. Help Steve out by finding >and printing
Note: If the final string is empty, print
Input Format
A single string,
Constraints
\$1 ≤ n ≤ 100\$
Output Format
If the final string is empty, print Empty String; otherwise, print the final >non-reducible string.
Sample Input
Sample Output
Solution Code
This is a brute-force solution, as I feel kind of confused when dealing with string algorithms. Hope for some good suggestions here.
Problem Statement
Steve has a string,
S, consisting of n lowercase English alphabetic >letters. In one operation, he can delete any pair of adjacent letters with same >value. For example, string "aabcc" would become either "aab" or "bcc" >after operation.Steve wants to reduce as much as possible. To do this, he will repeat the >above operation as many times as it can be performed. Help Steve out by finding >and printing
S's non-reducible form!Note: If the final string is empty, print
Empty String.Input Format
A single string,
S.Constraints
\$1 ≤ n ≤ 100\$
Output Format
If the final string is empty, print Empty String; otherwise, print the final >non-reducible string.
Sample Input
aaabccdddSample Output
abdSolution Code
public class Solution {
private static String solve(String input) {
int len = input.length();
int i = 0;
while (i < len - 1) {
char current = input.charAt(i);
char next = input.charAt(i+1);
if (current == next) {
input = input.substring(0, i) + input.substring(i+2);
len = input.length();
i = 0;
continue;
}
i++;
}
if (input.length() == 0) {
return "Empty String";
}
return input;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println(solve(s.next()));
}
}This is a brute-force solution, as I feel kind of confused when dealing with string algorithms. Hope for some good suggestions here.
Solution
Doc comments for public methods are indispensable.
I'd probably just use a foreach-loop and a DIY stack:
I'd probably just use a foreach-loop and a DIY stack:
/** @return input stripped of all abutting identical characters */
static String solve(String input) {
char []remains = new char[input.length()];
int top = -1; // highest valid index
for (char c: input.toCharArray())
if (top < 0 || c != remains[top])
remains[++top] = c;
else
--top;
return top < 0 ? "Empty String"
: new String(remains, 0, top+1);
}Code Snippets
/** @return input stripped of all abutting identical characters */
static String solve(String input) {
char []remains = new char[input.length()];
int top = -1; // highest valid index
for (char c: input.toCharArray())
if (top < 0 || c != remains[top])
remains[++top] = c;
else
--top;
return top < 0 ? "Empty String"
: new String(remains, 0, top+1);
}Context
StackExchange Code Review Q#150921, answer score: 5
Revisions (0)
No revisions yet.