blog.itcode.devblog.itcode.dev

[Programmers / JAVA] Level 1 Sort Strings by Custom Rule (12915)

Given a list of strings, strings, and an integer n, you want to sort the strings in ascending order based on the character at index n of each string. For example, if strings is ["sun", "bed", "car"] and n is 1, strings is sorted based on the characters "u", "e", "a" at index 1 of each word.

[Programmers / JAVA] Level 1 Sort Strings by Custom Rule (12915)

Given a list of strings, strings, and an integer n, you want to sort the strings in ascending order based on the character at index n of each string. For example, if strings is ["sun", "bed", "car"] and n is 1, strings is sorted based on the characters "u", "e", "a" at index 1 of each word.
RWB0104
@RWBwritten at 2021-12-16 11:48:21
Programmers

시리즈 모아보기

Programmers

40 / 78
RankLanguage Used
Level 1

🖼️ JAVA

🔗 Sort Strings by Custom Rule

Given a list of strings, strings, and an integer n, you want to sort the strings in ascending order based on the character at index n of each string. For example, if strings is [ "sun", "bed", "car" ] and n is 1, strings is sorted based on the characters "u", "e", "a" at index 1 of each word.

  • strings is an array of length 1 to 50, inclusive.
  • The elements of strings consist of lowercase alphabet letters.
  • The elements of strings are strings of length 1 to 100, inclusive.
  • Every element of strings has a length greater than n.
  • If multiple strings share the same character at index 1, the one that comes first alphabetically is placed first.
stringsnreturn
{ "sun", "bed", "car" }1{ "car", "bed", "sun" }
{ "abce", "abcd", "cdx" }2{ "abcd", "abce", "cdx" }

Input/Output Example 1

The values at index 1 of "sun", "bed", "car" are "u", "e", "a" respectively. Sorting strings based on these gives ["car", "bed", "sun"].

Input/Output Example 2

The values at index 2 of "abce", "abcd", and "cdx" are "c", "c", "x". So after sorting, "cdx" is placed last. Since "abce" and "abcd" tie, sorting them alphabetically puts "abcd" first, so the answer is ["abcd", "abce", "cdx"].

You can solve this easily by turning strings into a stream and sorting it. Sort based on the nth character of each element, and if the characters are the same, sort by the whole string alphabetically before returning.

In this case, it's convenient to first sort all the elements alphabetically once, and then sort again based on the nth character.

JAVA

import java.util.Arrays;
import java.util.Comparator;

/**
 * Sort Strings by Custom Rule class
 *
 * @author RWB
 * @since 2021.12.13 Mon 14:22:36
 */
class Solution
{
	/**
	 * Method that returns the answer
	 *
	 * @param strings: [String[]] array of strings
	 * @param n: [int] the index to sort by
	 *
	 * @return [String[]] the answer
	 */
	public String[] solution(String[] strings, int n)
	{
		return Arrays.stream(strings).sorted().sorted(Comparator.comparingInt(o -> o.charAt(n))).toArray(String[]::new);
	}
}
# Programmers# Algorithm# JAVA# Level 1
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08