408. Group Buddy Strings
Asked in
Group Buddy Strings

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.

Main Method

groupBuddyStrings

List<List<String>> groupBuddyStrings(List<String> strs)

Parameters

  • strs: A list of non-empty lowercase English strings.

Returns

Return the deterministically ordered groups of buddy strings.

First Follow-up Method

groupSingleCharacterBuddyStrings

List<List<String>> groupSingleCharacterBuddyStrings(List<String> strs)

Parameters

  • strs: A list in which every string contains exactly one lowercase English letter.

Returns

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.

Second Follow-up Method

areBuddyStrings

boolean areBuddyStrings(String first, String second)

Parameters

  • first: The first lowercase English string.
  • second: The second lowercase English string.

Returns

Return true if the strings are buddies and false otherwise. Cyclic wraparound must be used, so the distance from 'z' to 'a' is 1.

Final Follow-up Method

groupAsciiBuddyStrings

List<List<String>> groupAsciiBuddyStrings(List<String> strs)

Parameters

  • strs: A list of non-empty strings whose characters have values from 0 through 255.

Returns

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.

Constraints

  • 1 ≤ strs.size() ≤ 10,000
  • 1 ≤ strs.get(i).length() ≤ 1,000
  • The total number of characters across all input strings does not exceed 100,000.
  • For groupBuddyStrings, groupSingleCharacterBuddyStrings, and areBuddyStrings, every character is between 'a' and 'z'.
  • Every string passed to groupSingleCharacterBuddyStrings has length 1.
  • For groupAsciiBuddyStrings, every character value is between 0 and 255.
  • Strings can contain duplicates, and every occurrence must appear in the output.

Examples

Example 1

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].

Example 2

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.

Example 3

areBuddyStrings(first = "zab", second = "bcd")

Output: true

Both strings produce cyclic distances [1, 1].

Example 4

areBuddyStrings(first = "acf", second = "dfh")

Output: false

The first string produces distances [2, 3], while the second produces [2, 2].

Example 5

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.



Please use Laptop/Desktop or any other large screen to add/edit code.