返回介绍

solution / 1400-1499 / 1487.Making File Names Unique / README_EN

发布于 2024-06-17 01:03:19 字数 5610 浏览 0 评论 0 收藏 0

1487. Making File Names Unique

中文文档

Description

Given an array of strings names of size n. You will create n folders in your file system such that, at the ith minute, you will create a folder with the name names[i].

Since two files cannot have the same name, if you enter a folder name that was previously used, the system will have a suffix addition to its name in the form of (k), where, k is the smallest positive integer such that the obtained name remains unique.

Return _an array of strings of length _n where ans[i] is the actual name the system will assign to the ith folder when you create it.

 

Example 1:

Input: names = ["pes","fifa","gta","pes(2019)"]
Output: ["pes","fifa","gta","pes(2019)"]
Explanation: Let's see how the file system creates folder names:
"pes" --> not assigned before, remains "pes"
"fifa" --> not assigned before, remains "fifa"
"gta" --> not assigned before, remains "gta"
"pes(2019)" --> not assigned before, remains "pes(2019)"

Example 2:

Input: names = ["gta","gta(1)","gta","avalon"]
Output: ["gta","gta(1)","gta(2)","avalon"]
Explanation: Let's see how the file system creates folder names:
"gta" --> not assigned before, remains "gta"
"gta(1)" --> not assigned before, remains "gta(1)"
"gta" --> the name is reserved, system adds (k), since "gta(1)" is also reserved, systems put k = 2. it becomes "gta(2)"
"avalon" --> not assigned before, remains "avalon"

Example 3:

Input: names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"]
Output: ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"]
Explanation: When the last folder is created, the smallest positive valid k is 4, and it becomes "onepiece(4)".

 

Constraints:

  • 1 <= names.length <= 5 * 104
  • 1 <= names[i].length <= 20
  • names[i] consists of lowercase English letters, digits, and/or round brackets.

Solutions

Solution 1

class Solution:
  def getFolderNames(self, names: List[str]) -> List[str]:
    d = defaultdict(int)
    for i, name in enumerate(names):
      if name in d:
        k = d[name]
        while f'{name}({k})' in d:
          k += 1
        d[name] = k + 1
        names[i] = f'{name}({k})'
      d[names[i]] = 1
    return names
class Solution {
  public String[] getFolderNames(String[] names) {
    Map<String, Integer> d = new HashMap<>();
    for (int i = 0; i < names.length; ++i) {
      if (d.containsKey(names[i])) {
        int k = d.get(names[i]);
        while (d.containsKey(names[i] + "(" + k + ")")) {
          ++k;
        }
        d.put(names[i], k);
        names[i] += "(" + k + ")";
      }
      d.put(names[i], 1);
    }
    return names;
  }
}
class Solution {
public:
  vector<string> getFolderNames(vector<string>& names) {
    unordered_map<string, int> d;
    for (auto& name : names) {
      int k = d[name];
      if (k) {
        while (d[name + "(" + to_string(k) + ")"]) {
          k++;
        }
        d[name] = k;
        name += "(" + to_string(k) + ")";
      }
      d[name] = 1;
    }
    return names;
  }
};
func getFolderNames(names []string) []string {
  d := map[string]int{}
  for i, name := range names {
    if k, ok := d[name]; ok {
      for {
        newName := fmt.Sprintf("%s(%d)", name, k)
        if d[newName] == 0 {
          d[name] = k + 1
          names[i] = newName
          break
        }
        k++
      }
    }
    d[names[i]] = 1
  }
  return names
}
function getFolderNames(names: string[]): string[] {
  let d: Map<string, number> = new Map();
  for (let i = 0; i < names.length; ++i) {
    if (d.has(names[i])) {
      let k: number = d.get(names[i]) || 0;
      while (d.has(names[i] + '(' + k + ')')) {
        ++k;
      }
      d.set(names[i], k);
      names[i] += '(' + k + ')';
    }
    d.set(names[i], 1);
  }
  return names;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文