• + 0 comments

    I thought about using a map to count the letters, but even then I would have to know when the letters would be out of order and there would be no space to sort them. I chose to simply count how many consecutive letters are present after sorting the array (if there is free space for that)

    		int count = 0;
    		char[] chars = b.toCharArray();
    
    		if (b.indexOf('_') >= 0)
    			Arrays.sort(chars);
    
    		for (int i = 1; i < chars.length; ++i) {
    			if (chars[i] == '_')
    				break;
    
    			if (chars[i - 1] == chars[i]) {
    				++count;
    			} else if (count == 0) {
    				count = -1;
    				break;
    			} else {
    				count = 0;
    			}
    
    		}
    
    		return count > 0 || chars[0] == '_' ? "YES" : "NO";