- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写一个函数,它接受两个字符串,并且应该从一个或两个字符串中删除字符,直到两个字符串具有完全相同的字符。然后,我应该返回需要删除的字符数以实现两者的字谜状态。我得到了奇怪的输出(并通过了一个测试用例)。谁能告诉我我的功能哪里出错了?
public class Solution {
static int makeAnagram(String a, String b, int aLeng, int bLeng) {
StringBuilder stringA = new StringBuilder(a);
StringBuilder stringB = new StringBuilder(b);
int result=0;
for (int i=0; i<stringB.length();i++)
{
if (a.contains(b.substring(i)))
continue;
else
stringB.deleteCharAt(i);
}
for (int i=0; i<stringA.length();i++)
{
//if(stringB.toString().contains(stringA.toString().substring(i)))
if (b.contains(a.substring(i)))
continue;
else
stringA.deleteCharAt(i);
}
if (stringA.length()==stringB.length())
result = (aLeng-stringA.length()) + (bLeng-stringB.length());
else
result = -1;
return result;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String a = scanner.nextLine();
String b = scanner.nextLine();
int aLeng = a.length();
int bLeng = b.length();
int res = makeAnagram(a, b, aLeng, bLeng);
bufferedWriter.write(String.valueOf(res));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
测试用例 1
Input (stdin)
cde
abc
Your Output (stdout)
4
Expected Output
4
测试用例 2
Input (stdin)
fcrxzwscanmligyxyvym
jxwtrhvujlmrpdoqbisbwhmgpmeoke
Your Output (stdout)
-1
Expected Output
30
Compiler Message
Wrong Answer
测试用例 3
Input (stdin)
showman
woman
Your Output (stdout)
6
Expected Output
2
Compiler Message
Wrong Answer
最佳答案
您不需要使用子字符串,因为字符串越大,成本越高。您可以使用以下算法。我建议将名称命名为 checkAnagram 而不是 makeAnagram
string_counter_map = {}
for char in string1:
if char in string_counter_map:
string_counter_map[char] += 1
for char in string2:
if char in string_counter_map:
string_counter_map[char] -= 1
else:
string_counter_map[char] = 1
假设如果所有字符 string_counter_map 都包含零,则它是变位词,否则就不是。
关于java - CTCI Making Anagrams - 得到不正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52861434/
我正在寻找一种有效的解决方案来检查两个字符串是否是字谜词,但字符表/字典检查对于 unicode 可能不是一个好的解决方案。我已经提出了一个解决方案,但我不知道如何在数学上证明它是正确的。公式表达为“
这是我的代码,用于判断两个字符串是否是字谜 static boolean isAnagram(String a, String b) { if (a.length() != b.length(
为什么代码向我显示缺少 return 语句的错误? 我想做的是检查字符串的长度及其内容并比较它们。 import java.util.Scanner; public class Solution {
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我正在尝试解决古老的字谜问题。感谢那里的许多教程,我能够遍历一组字符串,递归地找到所有排列,然后将它们与英语单词列表进行比较。我发现的问题是,在大约三个词之后(通常是“变形”之类的词),我得到了 Ou
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 个月前。 Improve
完全披露我是一名正在做家庭作业的大学生。我不一定是在寻找我的问题的直接答案,而是在寻找正确方向的插入力。所以这是我的问题。我必须编写一个接受 2 个命令行参数的 C 程序,一个是包含单词列表的文件,另
我试图执行变位词排序,直到这里我才这样做: 我对数组中的每个string进行了排序,并将其存储到hashmap中。 现在我有一个映射,键是字符串,值是该字符串的变位词。 代码如下: public st
我在 Java 中有一个 Strings 数组。我需要从数组中找到字谜并将它们打印到屏幕上。 我在比较数组元素以检查它们是否是字谜的部分遇到了困难。我该怎么做?显然,我必须创建一个循环才能遍历数组。
我可以弄清楚如何创建字符串的变位词,但我不知道如何将它们与真实单词词典进行比较以检查变位词是否是真实单词。 Java API 中是否有包含整个英语词典的类? 最佳答案 没有,但是你可以从 variou
题目地址:https://leetcode.com/problems/anagrams/#/descriptionopen in new window 题目描述 Given an array of
题目地址:https://leetcode.com/problems/valid-anagram/open in new window Total Accepted: 78186 Total Sub
我有一个分配给一个函数,该函数将接受 2 个字符串并返回需要删除的字符数,以便使 2 个字符串彼此变位。我的问题是这个函数的时间复杂度是多少以及是否有更快的方法来达到相同的结果。这是我的解决方案: f
问题如下: 通过将 CARE 一词中的每个字母分别替换为 1、2、9 和 6,我们形成一个平方数:1296 = 36^(2)。值得注意的是,通过使用相同的数字替换,字谜词 RACE 也形成了一个平方数
我在 Cracking The Coding Interview 书中遇到了这个主题。挑战在于找到给定的较小字符串 s 在较大字符串 b 中的排列。我可以提出以下算法,其时间复杂度为 O(B x S)
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
正如您在标题中看到的,我正在尝试编写一个程序,该程序可以为我解决“晚间标准管测验”。它的一部分。它必须能够在地铁站名称中找到字谜。我在网上找到了很多代码来检查两个单词是否是字谜。但我需要一些不同的东西
我决定为我的父亲制作一个字谜解算器。我对编程很陌生,但我认为我仍然可以做到。我的成品可以用,但是速度确实很慢,例如找到8个字符的所有组合花了大约15分钟以上。我正在寻找对其进行优化/使其更快的方法。
这是一个解决以下问题的程序:“给定两个字符串 和 ,其长度可能相同也可能不同,确定制作 和 字谜所需的最小字符删除数。可以从其中删除任何字符任一字符串”。最后,两个字符串应该具有相同的字母,并且每个字
这是一个 Java 程序,可以在不使用数组的情况下生成和打印四位数字的所有可能的“Anagrams”。到目前为止,这是我能够做的: import java.util.*; class Anag {
我是一名优秀的程序员,十分优秀!