Click to See Complete Forum and Search --> : Need help eliminating multiples letters
blinksumgreen
September 10th, 2009, 01:53 PM
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.
nuzzle
September 10th, 2009, 03:44 PM
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.
Xeel
September 10th, 2009, 03:45 PM
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.
holestary
September 10th, 2009, 05:41 PM
i have a code, counts the chars..u can edit codes like what u want..
i hope to be helpful..
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:
input string :access
a-->1
c-->2
e-->1
s-->2
nuzzle
September 11th, 2009, 02:51 AM
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,
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();
}
blinksumgreen
September 11th, 2009, 01:39 PM
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.
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);
}
dlorde
September 11th, 2009, 02:14 PM
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
blinksumgreen
September 11th, 2009, 04:11 PM
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.
keang
September 11th, 2009, 05:22 PM
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.
blinksumgreen
September 11th, 2009, 07:26 PM
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
nuzzle
September 12th, 2009, 12:29 AM
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,
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.
keang
September 12th, 2009, 04:40 AM
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.
blinksumgreen
September 12th, 2009, 12:09 PM
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.
keang
September 12th, 2009, 12:12 PM
Don't worry we've all done it at some time or other. The important thing is to have learned from it.
blinksumgreen
September 14th, 2009, 12:05 PM
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.
keang
September 14th, 2009, 04:19 PM
When faced with problems like this the best approach is to write out the sequence of steps you would have to do if you had to do this.
Imagine you were given a board with an empty 5 x 5 grid on it and a load of scrabble letters lined up in 2 rows as per your example. Now imagine a friend in some far away place has exactly the same grid and letters. Your task is to telephone your friend and talk him/her through the job of moving each of the letters from the keyword and placing them into the grid. Then do the same for each of the letters in the alphabet. You now have the logic required for your program.
nuzzle
September 14th, 2009, 07:38 PM
---
nuzzle
September 14th, 2009, 07:41 PM
I'm in the final stages of this project and I have one more question.
Then you should start a new thread.
And remember that a proper problem formulation is the first step to a solution.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.