713590. Minimum Days

Medium

Minimum Days Slug: minimum-days--170646 Difficulty: Medium Id: 713590 Topic Tags: Strings, permutation, Data Structures Company Tags: None

Summary

The Minimum Days problem involves finding the minimum number of days required to make a given string of characters unique. This is achieved by rearranging the characters in the string to form all possible permutations and then counting the number of days it takes for each character to appear once. The key concepts involved are permutation, strings, and data structures.

Detailed Explanation

The approach to solving this problem involves generating all possible permutations of the given string using a backtracking algorithm. Each permutation is checked to see if it contains any duplicate characters. If no duplicates are found, then the number of days required for that permutation to appear in the sequence increases by one. The overall minimum number of days required is the maximum number of days found across all permutations.

Here's a step-by-step breakdown of the solution:

  1. Initialize an integer variable min_days to store the minimum number of days required.
  2. Define a recursive function permutations that generates all possible permutations of the given string and checks for duplicates.
  3. Inside the permutations function:
    • For each character in the permutation, check if it's already been seen before.
    • If not, increment a counter days.
  4. Compare the current days with the stored min_days. Update min_days if necessary.
  5. Return the minimum number of days required.

Time complexity: O(n!), where n is the length of the string, because we're generating all possible permutations of the string. Space complexity: O(n), as we need to store the current permutation and other variables.

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.*;

public class MinimumDays {
    public static int minDays(String str) {
        int n = str.length();
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        
        int days = 1;
        for (int i = 0; i < n; i++) {
            if (chars[i] == chars[i - 1]) {
                continue;
            }
            days++;
        }
        
        return days;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.