O(n) Java Solution
Same idea with One pass O(N), Straight Forward\n\nclass Solution {\n    public int uniqueLetterString(String S) {\n        List<Integer>[] record = new List[128
Same idea with [One pass O(N), Straight Forward](https://leetcode.com/problems/unique-letter-string/discuss/128952/One-pass-O(N)-Straight-Forward)\n```\nclass Solution {\n    public int uniqueLetterString(String S) {\n        List<Integer>[] record = new List[128];\n        int M = 1000000007;\n        int n = S.length();\n        for (int i = 0; i < 128; i++) {\n            record[i] = new ArrayList<>();\n        }\n        for (int i = 0; i < S.length(); i++) {\n            record[S.charAt(i)].add(i);\n        }\n        long result = 0;\n        for (int i = 0; i < 128; i++) {\n            int size = record[i].size();\n            for (int j = 0; j < size; j++) {\n                int index = record[i].get(j);\n                int left = j == 0 ? -1 : record[i].get(j - 1);\n                int right = j == size - 1 ? n : record[i].get(j + 1);\n                result += (index - left) * (right - index);\n                result %= M;\n            }\n        }\n        return (int)result;\n    }\n}\n```
[]