Given an array of desired String in the order of their creation. Since two strings cannot be equal replace it versioning them

huangapple go评论75阅读模式
英文:

Given an array of desired String in the order of their creation. Since two strings cannot be equal replace it versioning them

问题

给定一个按创建顺序排列的所需字符串数组由于两个字符串不能相等后出现的字符串将在其名称后面以 (k) 的形式添加其中 k 是最小的正整数使得所获得的名称尚未被使用

static String[] fileNaming(String[] names) {
    List<String> newNames = new ArrayList<String>();
    LinkedHashMap<String, Integer> wordCount = new LinkedHashMap<String, Integer>();
    Arrays.asList(names).stream().forEach(x -> {
        String str = x;
        if (wordCount.containsKey(x)) {
            str = x + "(" + String.valueOf(wordCount.get(x)) + ")";
            wordCount.merge(x, 1, Integer::sum);
            wordCount.merge(str, 1, Integer::sum);
        } else {
            wordCount.put(x, 1);
        }

        newNames.add(str);
        System.out.println(wordCount);
    });

    return newNames.toArray(new String[names.length]);
}

使用给定的代码对两个字符串数组进行测试

Arrays.toString(fileNaming(new String[] { "doc", "doc",
         "image", "doc(1)", "doc" }))

Arrays.toString(
            fileNaming(new String[] { "a(1)", "a(6)", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a" }))

两者的期望输出应为

["doc", "doc(1)", "image", "doc(1)(1)", "doc(2)"].

["a(1)", "a(6)",  "a",  "a(2)",  "a(3)",  "a(4)", "a(5)",  "a(7)",  "a(8)",  "a(9)",  "a(10)", "a(11)"]

通过给定的代码我能够正确匹配第一个输出但对于第二个输出我得到了

[a(1), a(6), a, a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9)]

在这里我们可以看到 a(1)a(6) 被重复了我已经进行了检查但仍然没有得到期望的输出
英文:

Given an array of desired String in the order of their creation. Since two strings cannot be equal , the one which comes later will have an addition to its name in a form of (k), where k is the smallest positive integer such that the obtained name is not used yet.

static String[] fileNaming(String[] names) {
List&lt;String&gt; newNames = new ArrayList&lt;String&gt;();
LinkedHashMap&lt;String, Integer&gt; wordCount = new LinkedHashMap&lt;String, Integer&gt;();
Arrays.asList(names).stream().forEach(x -&gt; {
String str = x;
if (wordCount.containsKey(x)) {
str = x + &quot;(&quot; + String.valueOf(wordCount.get(x)) + &quot;)&quot;;
wordCount.merge(x, 1, Integer::sum);
wordCount.merge(str, 1, Integer::sum);
} else {
wordCount.put(x, 1);
}
newNames.add(str);
System.out.println(wordCount);
});
return newNames.toArray(new String[names.length]);
}

Testing this code against two string[]'s

Arrays.toString(fileNaming(new String[] { &quot;doc&quot;, &quot;doc&quot;,
&quot;image&quot;, &quot;doc(1)&quot;, &quot;doc&quot; }))
Arrays.toString(
fileNaming(new String[] { &quot;a(1)&quot;, &quot;a(6)&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot; }))

The desired output for both of them should be

[&quot;doc&quot;, &quot;doc(1)&quot;, &quot;image&quot;, &quot;doc(1)(1)&quot;, &quot;doc(2)&quot;].
[&quot;a(1)&quot;, &quot;a(6)&quot;,  &quot;a&quot;,  &quot;a(2)&quot;,  &quot;a(3)&quot;,  &quot;a(4)&quot;, &quot;a(5)&quot;,  &quot;a(7)&quot;,  &quot;a(8)&quot;,  &quot;a(9)&quot;,  &quot;a(10)&quot;, &quot;a(11)&quot;]

With the given code I was able to match the first output correctly. But for the second one I am getting

[a(1), a(6), a, a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9)]

Here we can see that a(1) and a(6) are being repeated. For which I have put a check but still no getting the desired output.

答案1

得分: 1

问题在于你没有检查新文件是否已经存在。我认为它可以这样工作:

static String[] fileNaming(String[] names) {
    List<String> newNames = new ArrayList<String>();
    var usedNames = new HashSet<String>();
    Arrays.asList(names).stream().forEach(x -> {
        int retries = 0;
        var uniqueName = x;
        while (!usedNames.add(uniqueName)) {
            uniqueName = x + "(" + ++retries + ")";
        }
        newNames.add(uniqueName);
        System.out.println(wordCount);
    });

    return newNames.toArray(new String[names.length]);
}

如果有许多相同的字符串副本,这会比你的原始代码慢...但它更简单并且有效,所以它有这个优点!在这种情况下,你可以微调这段代码以提高速度,但可能不值得(除非你知道你会得到许多字符串的副本)。

编辑:哦,糟糕,这是 O(n) 版本:

static String[] fileNaming(String[] names) {
    List<String> newNames = new ArrayList<String>();
    var usedNames = new HashSet<String>();
    var counts = new HashMap<String, Integer>();
    Arrays.asList(names).stream().forEach(x -> {
        int retries = counts.getOrDefault(x, 0);
        var uniqueName = x;
        while (!usedNames.add(uniqueName)) {
            uniqueName = x + "(" + ++retries + ")";
        }
        newNames.add(uniqueName);
        counts.put(x, retries);
        System.out.println(wordCount);
    });

    return newNames.toArray(new String[names.length]);
}
英文:

The problem is that you aren't checking your new files to see if they are already present. I think it can work like this:

static String[] fileNaming(String[] names) {
List&lt;String&gt; newNames = new ArrayList&lt;String&gt;();
var usedNames = new HashSet&lt;String&gt;();
Arrays.asList(names).stream().forEach(x -&gt; {
int retries = 0;
var uniqueName = x;
while (!usedNames.add(uniqueName)) {
uniqueName = x + &quot;(&quot; + ++retries + &quot;)&quot;;
}
newNames.add(uniqueName);
System.out.println(wordCount);
});
return newNames.toArray(new String[names.length]);
}

If you have a lot of copies of the same string, this will be slower than your original code...but it is simpler and it works, so it has that going for it! You could tweak this code to be faster in that case but it probably isn't worth it (unless you know you'll get lots of copies of your strings).

Edit: Oh heck, here's the O(n) version:

static String[] fileNaming(String[] names) {
List&lt;String&gt; newNames = new ArrayList&lt;String&gt;();
var usedNames = new HashSet&lt;String&gt;();
var counts = new HashMap&lt;String, Integer&gt;();
Arrays.asList(names).stream().forEach(x -&gt; {
int retries = counts.getOrDefault(x, 0);
var uniqueName = x;
while (!usedNames.add(uniqueName)) {
uniqueName = x + &quot;(&quot; + ++retries + &quot;)&quot;;
}
newNames.add(uniqueName);
counts.put(x, retries);
System.out.println(wordCount);
});
return newNames.toArray(new String[names.length]);
}

huangapple
  • 本文由 发表于 2020年10月16日 01:53:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64377192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定