Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int firstUniqChar(String s) {
- int bestIndex = 1000000000;
- for (char c = 'a'; c <= 'z'; c++) {
- int first = s.indexOf(c);
- int last = s.lastIndexOf(c);
- if (first != -1 && first == last) {
- bestIndex = Math.min(bestIndex, first);
- }
- }
- if (bestIndex == 1000000000) bestIndex = -1;
- return bestIndex;
- }
- }
- class Solution {
- public int firstUniqChar(String s) {
- int freq[] = new int[26];
- for (int i = 0; i < s.length(); i++) {
- char c = s.charAt(i);
- freq[c - 'a']++;
- }
- for (int i = 0; i < s.length(); i++) {
- char c = s.charAt(i);
- if (freq[c - 'a'] == 1) {
- return i;
- }
- }
- return -1;
- }
- }
- class Solution {
- public int firstUniqChar(String s) {
- for (int i = 0; i < s.length(); i++) {
- char c = s.charAt(i);
- if (s.indexOf(c) == s.lastIndexOf(c)) {
- return i;
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement