0

For example, searching in the string "aabacbba"for the substring"a"would return the index of3`:

private int findString(String str, int start) {
   // TODO: Implement string search
}

I'm not sure how to check to make sure the string is alone and repeating.

Chris
  • 6,761
  • 6
  • 52
  • 67
Frank
  • 339
  • 8
  • 18
  • Will it only be searching for single characters? – Ben N May 04 '15 at 00:45
  • Ya, and it's given a start point so you could start at any index, then search for the first single occurrence of the character. – Frank May 04 '15 at 00:49
  • Have you tried anything so far? Nobody will write the entire solution for you if you haven't tried anything. – camelsaucerer May 04 '15 at 00:53
  • for(int i = 0; i < line.length(); i++) { if(line.charAt(i) == str) return i; } else return -1; – Frank May 04 '15 at 00:56
  • I'm not too sure how to check if the character is alone and how to set the start point – Frank May 04 '15 at 00:57
  • Can you add all code to your question so it is easier for everyone to see? – camelsaucerer May 04 '15 at 00:57
  • i changed it to try for (int i = start; i < line.length(); i++) { if (!(line.substring(i-1,i).equals(str) && line.substring(i+1,i+2).equals(str))) { return i; } else { return -1; } } } – Frank May 04 '15 at 01:02
  • [Relevant](http://stackoverflow.com/questions/2285533/find-the-first-un-repeated-character-in-a-string). Should give you a few ideas. – Jyr May 04 '15 at 01:02

4 Answers4

0

Here are some things to help you:

Strings have a s.charAt(i); method that returns a character at the given index.

A String also has a length() method that returns the amount of character it has.

You can use a for loop to check each character and see if it matches the one you want.

for(int i = 0; i < line.length(); i++)

Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75
0

Using substring() and indexOf() makes this really easy

public static void main(String[] args) throws Exception {
    String testString = "aabacbba";
    System.out.println(findString(testString, 2, "c"));
}

private static int findString(String stringToSearch, int start, String stringToFind) {
    int index = stringToSearch.substring(start).indexOf(stringToFind);
    
    // Add start to the result of indexOf to have the actual index in the original string
    // It'll return a -1 if no result is found
    return index > -1 ? index + start : index;        
}

Results:

findString(testString, 2, "a") => returns 3

findString(testString, 2, "c") => returns 4

findString(testString, 2, "d") => returns -1

findString(testString, 1, "bb") => returns 5

UPDATE

After reading a comment about checking to make sure the character is a single occurrence after the start location, I've made an update answer.

public static void main(String[] args) throws Exception {
    String testString = "aabacbba";
    System.out.println(findString(testString, 3, "b"));
}

private static int findString(String stringToSearch, int start, String stringToFind) {
    int index = stringToSearch.substring(start).indexOf(stringToFind);
    
    // Check the next character to make sure it's a single occurrence
    if (index > -1 && stringToSearch.charAt(index + start + 1) == stringToFind.charAt(0)) {
        index = -1;
    }
    
    // Add start to the result of indexOf to have the actual index in the original string
    // It'll return a -1 if no result is found
    return index > -1 ? index + start : index;
}

Results:

findString(testString, 3, "b") => returns -1, because b repeats

findString(testString, 2, "a") => returns 3

findString(testString, 2, "c") => returns 4

Community
  • 1
  • 1
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

I believe what you are going to find is, for a character, you are going to find in the first position that is not repetitive. Is my understanding correct?

One simple is to use regex to find out such character.

e.g.

shorter but trickier to use regex + split

int position = "aabacbba".split("(?<!a)a(?!a)")[0].length();

or a proper use of matcher:

Matcher m = Pattern.compile("(?<!a)a(?!a)").matcher("aabacbba");
m.find();
int position = m.start();

Both will give you 3


However it is not going to be difficult to use String.chatAt(i) with a bit of logic to find that out

psuedo code

for (int i = 0; i < inputString.length; ++i) {
    if (inputString[i] == charToFind
        && inputString[i-1] != charToFind
        && inputString[i+1] != chatToFind) {
        return i;
    }
}
return -1;

Just a little trap for you to solve by yourself: inputString[i-1] and inputString[i+1] can go out of bound. It should be easy to fix by yourself.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
0

This should work:

private static int findString(String sub, String str, int start) {
    boolean isolated = true;
    int i = str.indexOf(sub, start);
    while (i >= 0) {
        int end = i + sub.length();
        int next = str.indexOf(sub, end);
        if (next < 0 || isolated && next > end) {
            break;
        }
        isolated = next > end;
        i = next;
    }
    return isolated ? i : -1;
}
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97