Need help eliminating multiples letters
I want to take in a string and eliminate any multiple letters. An example would be if I input "access", I would likes the output to be "aces". I tried turning the string into an array of characters and then checking each against itself, but I couldn't get it to work, and it didn't really seem like the most straight forward approach.
Re: Need help eliminating multiples letters
Quote:
Originally Posted by
blinksumgreen
I want to take in a string and eliminate any multiple letters.
You copy the input string to an output string, char by char. If the next char to copy equals the last char you copied you drop it.
Re: Need help eliminating multiples letters
The fastest and probably the easiest way would be to convert the string into a char array and work with char pairs. I hope you get my idea... The alternative to this would be regular expressions but imho it's more difficult approach for this case.
Re: Need help eliminating multiples letters
i have a code, counts the chars..u can edit codes like what u want..
i hope to be helpful..
Code:
import java.util.Scanner;
public class eleminate {
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
System.out.print("input string :");
String input=keyboard.nextLine().toLowerCase();
char ref;
for(int i=0;i<input.length();i++)
{
ref=input.charAt(i);
int s=1;
int o=0;
for(int k=0; k<i;k++){
if(ref==input.charAt(k))
o++;
}
if(o!=0) continue;
for(int j=i+1;j<input.length();j++){
if(ref==input.charAt(j))
s++;
}
System.out.println(ref+"-->"+s);
}
}
}
Output:
Code:
input string :access
a-->1
c-->2
e-->1
s-->2
Re: Need help eliminating multiples letters
Quote:
Originally Posted by
nuzzle
You copy the input string to an output string, char by char. If the next char to copy equals the last char you copied you drop it.
Something like this,
Code:
String eliminateMultiples(String s) {
if (s==null) return null;
final int N = s.length();
if (N==0) return "";
StringBuilder sb = new StringBuilder(N);
char last = s.charAt(0);
sb.append(last);
for (int i=1; i<N; i++) {
char ch = s.charAt(i);
if (ch != last) {
last = ch;
sb.append(ch);
}
}
return sb.toString();
}
Re: Need help eliminating multiples letters
I came up with the following, but I need some help with the logic. I think the problems lie in two main areas:
1. If you delete an item, the size will change but won't be able to update the loop.
2. Even if I do a test run with a word that has no repeating letters, such as "hey", where the length will definitely not change, it still prints out an incorrect statement.
Code:
public void removeMultiples(String x) {
String keyword = x;
StringBuffer sb, temp;
sb = new StringBuffer(keyword);
// reverses the keyword so that multiple letters are removed
// from the end, not the beginning
temp = sb.reverse();
for (int i = 0; i < temp.length(); i++) {
for (int j = 1; j < temp.length(); j++) {
char y = temp.charAt(i);
char z = temp.charAt(j);
// if what is at location y and z match, remove what is at z
if(y == z){
temp.deleteCharAt(j);
}
}
}
// corrects the orientation of the keyword
sb = temp.reverse();
System.out.println(sb);
}
Re: Need help eliminating multiples letters
Did you read Nuzzle's example?
Have you tried stepping through your code by hand with pencil and paper to see exactly what it does?
Optimism is an occupational hazard of programming: testing is the treatment...
K. Beck
Re: Need help eliminating multiples letters
Quote:
Originally Posted by
dlorde
Did you read Nuzzle's example?
Have you tried stepping through your code by hand with pencil and paper to see exactly what it does?
Optimism is an occupational hazard of programming: testing is the treatment...
K. Beck
Yes, Nuzzle's example only works sometimes. I have tried stepping through the code with paper and pencil and it seems like if there are no repeated letters in the word, then it should work fine, but it doesn't. And in the case of letters being removed, it somehow is able to remove letters and the beginning and end, but not in the middle. When I try and test it, it seems like it should be taking letters out of the beginning and middle, and be jumping out of the for-loop before it gets to the end.
Re: Need help eliminating multiples letters
Quote:
Yes, Nuzzle's example only works sometimes.
Really, what values doesn't it handle correctly?
Please provide some examples of what text doesn't work, remembering to tell us the input value, the output value and the expected value.
Re: Need help eliminating multiples letters
Quote:
Originally Posted by
keang
Really, what values doesn't it handle correctly?
Please provide some examples of what text doesn't work, remembering to tell us the input value, the output value and the expected value.
The tests I ran were:
Input~~~~~~~Output
hello ~~~~~~~ helo
everyday ~~~~ everyday
pigglywiggly ~~ piglywigly
The output for hello is correct.
everyday should come out as: evryda
pigglywiggly should come out as: piglyw
Re: Need help eliminating multiples letters
Quote:
Originally Posted by
blinksumgreen
The output for hello is correct.
everyday should come out as: evryda
pigglywiggly should come out as: piglyw
Well, I misunderstood your requirements. I thought repetitions were to be removed. Instead you want a list of all unique chars with the order of appearance preserved.
This can be achieved with a slight modification of my previous solution. You just replace the "last" variable with a scan of the output StringBuffer. If a char is already present it's not appended. Here's a pure String version following the same principle,
Code:
String eliminateMultiples2(String inStr) {
if (inStr==null) return null;
String outStr = "";
for (int i=0; i<inStr.length(); i++) {
char ch = inStr.charAt(i);
if (outStr.indexOf(ch)==-1) outStr = outStr + ch; // append input char to output String if not already present
}
return outStr;
}
Note that upper and lower case letters are treated as different letters.
Also not that the solution is quadratic in complexity (O(N*N)). If speed is essential there are linear (O(N)) versions.
Re: Need help eliminating multiples letters
Quote:
Originally Posted by nuzzle
Well, I misunderstood your requirements. I thought repetitions were to be removed.
You're not the only one. In the initial post the problem wasn't well explained and the example was misleading.
blinksumgreen: This just goes to show the importance of writing a well worded description of the problem and of giving good examples. A few extra minutes put into your original post would have saved you and us, a lot of time and effort.
Re: Need help eliminating multiples letters
Thank you for the help, and I'm sorry about the misunderstanding, that was all on me. I had been working on the problem for a while and in aggravated haste, I posted it on here. I assumed that since I knew what I meant, everyone else would magically as well.
Re: Need help eliminating multiples letters
Don't worry we've all done it at some time or other. The important thing is to have learned from it.
Re: Need help eliminating multiples letters
I'm in the final stages of this project and I have one more question.
Recape + New info - keyword has any non unique characters removed.
"Alphabet (without J, so as to keep it to 25 letters, and therefore a nice square)" has any characters that were in keyword removed from it.
Both of these are completed within other methods and it can be assumed that keyword will not have non unique characters, and that alphabet has already had the characters that are in keyword removed.
Fill in the table with the keyword by rows, and then fill in the rest of the table with the remaining alphabet, by columns. How do I do this? I've tried multidimensional arrays and using mod, but I can't quite figure it out.
Example:
Keyword: computer
alphabet: ABDFGHIKLNQSUVWXYZ
C O M P U
T E R N W
A F I Q X
B G K S Y
D H L V Z
Kind of a hodge podge, so if I didn't explain this well, please let me know.