0

I have a problem where I need to output whether or not a user inputted string has duplicate sets of characters in the suffix and prefix positions within the string, if it does I must print out "ok".

if the user inputs "ermarf" it would print "not ok"

My question is how do I code this. I thought using CharSequence() method would work; but with that you would have to indicate what specific characters to look for, and that wont work for what I'm trying to do; any suggestions?

Jose Luis
  • 3,307
  • 3
  • 36
  • 53
Enigma
  • 13
  • 3
  • Please provide examples of the strings with and without the duplicate sets of characters you wish to detect. – George Cummins Jan 28 '12 at 05:37
  • Oh, I forgot to mention its looking to match the prefix and suffix of the string, so basically, the user inputs "rerfgrer" the program would print out "ok" because the string "rerfrer" has the same characters for the prefix and suffix of the string. If the user input "ermare" it would print out "not ok" because the prefix and suffix of the string do not match – Enigma Jan 28 '12 at 05:55
  • how do you determine the prefix and suffix? Because if `e` is a valid prefix/suffix, then your counter example is flawed. – Yanick Rochon Jan 28 '12 at 06:02
  • You're right thats a typo on my part. I've updates the OP in hopes to better explain what I'm trying to accomplish. – Enigma Jan 28 '12 at 06:05
  • Are you trying to compare the first and the last characters? – Bhesh Gurung Jan 28 '12 at 06:07
  • I'm trying to compare the prefix and the suffix of the string. So the prefix could be more than one character long. For example "manama" would be "ok" and so would "abcdeabc" – Enigma Jan 28 '12 at 06:11
  • 1
    @Enigma, But why compare larger? if 1 character matches, it's enough to print ok. – st0le Jan 28 '12 at 08:06
  • @st0le What do you mean? – Enigma Jan 28 '12 at 08:18

3 Answers3

0

Your best bet is regular expression. See the duplicate answer here for inspiration.

How can I find repeated characters with a regex in Java

Community
  • 1
  • 1
CppLearner
  • 16,273
  • 32
  • 108
  • 163
0

You can use a Set of Characters.

e.g.

String input = "abcde";
Set<Character> set = new HashSet<Character>(); 
boolean hasDup = false; 
for(int i=0; i<input.length(); i++) {
    if(!set.add(input.charAt(i))) {
        hasDup = true;
        break;
    }
}
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • Could you explain what this code does? I'm new and really attempting to understand what it does exactly. – Enigma Jan 28 '12 at 05:55
  • It checks if the input string has duplicate character in it. But your requirement seem to entirely different than your OP. You need to update it with more examples. Because I read your comment and it's still not clear what you are really trying to achieve. – Bhesh Gurung Jan 28 '12 at 05:58
  • I've updated the OP for hopes of better describing what I'm trying to accomplish, please take a look! – Enigma Jan 28 '12 at 06:05
0

use set of strings, and fill it reading next char of main string. then check size of set.

dantuch
  • 9,123
  • 6
  • 45
  • 68
  • Oh, I forgot to mention its looking to match the prefix and suffix of the string, so basically, the user inputs "rerfgrer" the program would print out "ok" because the string "rerfrer" has the same characters for the prefix and suffix of the string. If the user input "ermare" it would print out "not ok" because the prefix and suffix of the string do not match – Enigma Jan 28 '12 at 05:52