Skip to main content
Changed according to comment
Source Link

A way to optimize it would be to return as soon as you have found a duplicate, unless the question specifically asks you to list all duplicates.

You could refactor it to something like that:

public static boolean checkUnique(String s) {
    int len = s.length();
    for (int i = 0; i < len; i++) {
        int c = s.codePointAt(i);
        int lastnext = s.lastIndexOfindexOf(c, i + 1);
        if (lastnext > i) {
             return false;
        }
    }
    return true;
}

It isn't more efficient as your solution, but I doubt there is an algorithm that can do it better.

BTW There is really no point to use ++i instead of i++.

A way to optimize it would be to return as soon as you have found a duplicate, unless the question specifically asks you to list all duplicates.

You could refactor it to something like that:

public static boolean checkUnique(String s) {
    int len = s.length();
    for (int i = 0; i < len; i++) {
        int c = s.codePointAt(i);
        int last = s.lastIndexOf(c);
        if (last > i) {
             return false;
        }
    }
    return true;
}

It isn't more efficient as your solution, but I doubt there is an algorithm that can do it better.

BTW There is really no point to use ++i instead of i++.

A way to optimize it would be to return as soon as you have found a duplicate, unless the question specifically asks you to list all duplicates.

You could refactor it to something like that:

public static boolean checkUnique(String s) {
    int len = s.length();
    for (int i = 0; i < len; i++) {
        int c = s.codePointAt(i);
        int next = s.indexOf(c, i + 1);
        if (next > i) {
             return false;
        }
    }
    return true;
}

It isn't more efficient as your solution, but I doubt there is an algorithm that can do it better.

added 1 character in body
Source Link

A way to optimize it would be to return as soon as you have found a duplicate, unless the question specifically asks you to list all duplicates.

You could refactor it to something like that:

public static boolean checkUnique(String s) {
    int len = s.length();
    for (int i = 0; i < len; i++)  {
        int c = s.codePointAt(i);
        int last = s.lastIndexOf(c);
        if (last > i) {
             return false;
        }
    }
    return true;
}

It isn't more efficient as your solution, but I doubt there is an algorithm that can do it better.

BTW There is really no point to use ++i instead of i++.

A way to optimize it would be to return as soon as you have found a duplicate, unless the question specifically asks you to list all duplicates.

You could refactor it to something like that:

public static boolean checkUnique(String s) {
    int len = s.length();
    for(int i = 0; i < len; i++)  {
        int c = s.codePointAt(i);
        int last = s.lastIndexOf(c);
        if (last > i) {
             return false;
       }
    }
    return true;
}

It isn't more efficient as your solution, but I doubt there is an algorithm that can do it better.

BTW There is really no point to use ++i instead of i++.

A way to optimize it would be to return as soon as you have found a duplicate, unless the question specifically asks you to list all duplicates.

You could refactor it to something like that:

public static boolean checkUnique(String s) {
    int len = s.length();
    for (int i = 0; i < len; i++) {
        int c = s.codePointAt(i);
        int last = s.lastIndexOf(c);
        if (last > i) {
             return false;
        }
    }
    return true;
}

It isn't more efficient as your solution, but I doubt there is an algorithm that can do it better.

BTW There is really no point to use ++i instead of i++.

added 4 characters in body
Source Link

A way to optimize it would be to return as soon as you have found a duplicate, unless the question specifically asks you to list all duplicates.

You could refactor it to something like that:

public static boolean checkUnique(String s) {
    for(int ilen = 0; i<ss.length();
    for(int i = 0; i < len; i++)  {
        charint c = s.charAtcodePointAt(i);
        int last = s.lastIndexOf(c);
        if (last > i) {
             return false;
       }
    }
    return true;
}

It isn't more efficient as your solution, but I doubt there is an algorithm that can do it better.

BTW There is really no point to use ++i instead of i++.

A way to optimize it would be to return as soon as you have found a duplicate, unless the question specifically asks you to list all duplicates.

You could refactor it to something like that:

public static boolean checkUnique(String s) {
    for(int i = 0; i<s.length(); i++)  {
        char c = s.charAt(i);
        int last = s.lastIndexOf(c);
        if (last > i) {
             return false;
       }
    }
    return true;
}

It isn't more efficient as your solution, but I doubt there is an algorithm that can do it better.

BTW There is really no point to use ++i instead of i++.

A way to optimize it would be to return as soon as you have found a duplicate, unless the question specifically asks you to list all duplicates.

You could refactor it to something like that:

public static boolean checkUnique(String s) {
    int len = s.length();
    for(int i = 0; i < len; i++)  {
        int c = s.codePointAt(i);
        int last = s.lastIndexOf(c);
        if (last > i) {
             return false;
       }
    }
    return true;
}

It isn't more efficient as your solution, but I doubt there is an algorithm that can do it better.

BTW There is really no point to use ++i instead of i++.

Source Link
Loading
Post Made Community Wiki by Dorian Gray