def game_sort(s, p): # Split the string s into p contiguous non-empty parts n = len(s) part_len = n // p parts = [s[i:i+part_len] for i in range(0, n, part_len)] if n % p != 0: parts[-1] += s[n-part_len:] # Sort the characters in each part for i in range(p): parts[i] = ''.join(sorted(parts[i])) # Check if the parts are sorted in non-decreasing lexicographical order for i in range(1, p): if parts[i] < parts[i-1]: return "NO" return "YES" # Example usage: s = "CODEJAM" p = 3 print(game_sort(s, p)) # Output: YES