Medium
You are given a string s consisting of lowercase English letters.
Return an integer denoting the maximum number of substring you can split s into such that each substring starts with a distinct character (i.e., no two substrings start with the same character).
Example 1:
Input: s = “abab”
Output: 2
Explanation:
"abab" into "a" and "bab".'a' and 'b'. Thus, the answer is 2.Example 2:
Input: s = “abcd”
Output: 4
Explanation:
"abcd" into "a", "b", "c", and "d".Example 3:
Input: s = “aaaa”
Output: 1
Explanation:
"aaaa" are 'a'.'a'. Thus, the answer is 1.Constraints:
1 <= s.length <= 105s consists of lowercase English letters.class Solution {
fun maxDistinct(s: String): Int {
var mask = 0
var res = 0
for (c in s.toCharArray()) {
val bit = 1 shl (c.code - 'a'.code)
if ((mask and bit) == 0) {
mask = mask or bit
res++
if (res == 26) {
break
}
}
}
return res
}
}