Two strings of equal length are called buddy strings when the cyclic distance between every corresponding pair of consecutive characters is the same.
For lowercase English letters, the cyclic distance from the character at index i - 1 to the character at index i is:
(str.charAt(i) - str.charAt(i - 1) + 26) % 26
Two strings with at least two characters are buddies if they have the same length and produce the same distance at every index from 1 to str.length() - 1.
For strings containing exactly one character, two strings are buddies only when their characters are equal. Therefore, duplicate single-character strings belong to the same group, while different characters belong to different groups.
Group all buddy strings together. Preserve the input order of strings within every group. Order the groups by the input position of their first member. Duplicate strings must be preserved.
List<List<String>> groupBuddyStrings(List<String> strs)
strs: A list of non-empty lowercase English strings.Return the deterministically ordered groups of buddy strings.
List<List<String>> groupSingleCharacterBuddyStrings(List<String> strs)
strs: A list in which every string contains exactly one lowercase English letter.Group identical single-character strings together. Different characters must be placed in different groups. Preserve input order within groups and order groups by the first occurrence of their character.
boolean areBuddyStrings(String first, String second)
first: The first lowercase English string.second: The second lowercase English string.Return true if the strings are buddies and false otherwise. Cyclic wraparound must be used, so the distance from 'z' to 'a' is 1.
List<List<String>> groupAsciiBuddyStrings(List<String> strs)
strs: A list of non-empty strings whose characters have values from 0 through 255.Group the strings using a cyclic alphabet of 256 character values. For each consecutive pair, calculate:
(str.charAt(i) - str.charAt(i - 1) + 256) % 256
Apply the same single-character, duplicate-preservation, and deterministic ordering rules used by the main method.
1 ≤ strs.size() ≤ 10,0001 ≤ strs.get(i).length() ≤ 1,000100,000.groupBuddyStrings, groupSingleCharacterBuddyStrings, and areBuddyStrings, every character is between 'a' and 'z'.groupSingleCharacterBuddyStrings has length 1.groupAsciiBuddyStrings, every character value is between 0 and 255. groupBuddyStrings(strs = List.of("ace", "bdf", "xyz", "yza", "aaa", "ccc", "abb"))
Output: List.of(List.of("ace", "bdf"), List.of("xyz", "yza"), List.of("aaa", "ccc"), List.of("abb"))
The first two strings have cyclic distances [2, 2]. The next two have distances [1, 1], including the wrap from 'z' to 'a'. The strings "aaa" and "ccc" both have distances [0, 0].
groupSingleCharacterBuddyStrings(strs = List.of("m", "n", "m", "p", "n"))
Output: List.of(List.of("m", "m"), List.of("n", "n"), List.of("p"))
Identical single-character strings are grouped together, while different characters remain in separate groups.
areBuddyStrings(first = "zab", second = "bcd")
Output: true
Both strings produce cyclic distances [1, 1].
areBuddyStrings(first = "acf", second = "dfh")
Output: false
The first string produces distances [2, 3], while the second produces [2, 2].
groupAsciiBuddyStrings(strs = List.of("!#", "@B", "AA", "zz", "9?"))
Output: List.of(List.of("!#", "@B"), List.of("AA", "zz"), List.of("9?"))
The first group has cyclic distance 2, the second group has distance 0, and "9?" has distance 6.