情书字符串轮换公司测试编程题库测试用例通过,隐藏测试用例失败

问题描述 投票:0回答:10

我在名为mettl的招聘公司平台上进行了测试。

问题陈述:

你给你的朋友写了一封情书。然而,在你的朋友可以阅读它之前,其他人已经阅读了它,并将每个单词的字符旋转到正确的 K 次。找出即使在字母移动之后仍然保持不变的单词数量。

注意: 单词之间可以有多个空格。

输入规格:

输入1:单词字符串

输入2:旋转发生K次

输出规格:

你的函数应该返回正确单词的数量。

示例1:

输入1:llohe ereth

输入2: 2

输出:0

说明: 在示例 1 中,“llohe ereth”是一个 K 因子为 2 的旋转字符串。因此,将最后两个字母从右向左移动后,我们得到原始字符串“Hello there”。

示例2:

输入1:阿达达

输入2:3

输出: 1

解释: 在示例 2 中,“adaada”旋转 3 次后返回“adaada”。因此答案是 1。

我在下面写了解决方案,该解决方案通过了上述 2 个基本案例,但因隐藏测试用例而失败(还包括时间复杂度测试用例)。只有一个角落测试用例通过了,因为我正在检查 String input1 不为空。解决方法如下:

public int rotatedWords(String input1, int input2) {
    int count = 0;
    String arr[] = input1.split(" ");
    if (input1 != null && !input1.isEmpty()) {
        for (int i = 0; i < arr.length; i++) {
            String s1 = arr[i] + arr[i];
            int start = arr[i].length() - input2;

            System.out.println("arr[i] : " + arr[i]);
            String s2 = s1.substring(start, start + arr[i].length());
            System.out.println("s2 : " + s2);
            if (s2.equalsIgnoreCase(arr[i])) {
                count++;
            }
        }
    }
    return count;
}

问的是,我无法理解为什么隐藏的测试用例失败。请帮助我。

java string substring
10个回答
0
投票

这是Python 3中的解决方案

def rotatedWords(cls, input1,input2):
    rotateString = input1.split(' ')
    count = 0
    for i in range(1,input2+1):
        for j in range(len(rotateString)):
            rotateString[j]= rotateString[j][1:]+rotateString[j][0]
        
    actualWrod = input1.split(' ')

    for i in range(len(rotateString)):
        for j in range(len(actualWrod)):
            if rotateString[i]==actualWrod[j]:
                count+=1
    return count

print(rotatedWords(1,"adaada",3))
print(rotatedWords(1,"llohe ereth",2))

0
投票
#include<bits/stdc++.h>
using namespace std;
 
// In-place rotates s towards left by d
void leftrotate(string &s, int d)
{
    reverse(s.begin(), s.begin()+d);
    reverse(s.begin()+d, s.end());
    reverse(s.begin(), s.end());
}
 
// In-place rotates s towards right by d
int rightrotate(string &s, int d)
{  
  string s1="";
  int i=0,flag=0;
  vector<string>str;
  while(s[i]!='\0'){
    if(s[i] != ' '){
      s1 = s1+s[i];
      i++;
    }
    else{
      if(s1 != ""){ //checking for extra space
        str.push_back(s1);
        s1 = "";
        i++;
      }
      else
        i++;
    }
  }
  str.push_back(s1);
  vector<string>str2;
  for(int i=0;i<str.size();i++){
    str2.push_back(str[i]);
  }
  for(int i=0;i<str.size();i++){
    leftrotate(str[i], str[i].length()-d);
  }
  for(int i=0;i<str.size();i++){
    if(str[i] == str2[i]){
      flag++;
    }
  }
  return flag;
}
 
int main()
{
 
    string str1;
    getline(cin, str1);
    int k;
    cin>>k;
    cout<<rightrotate(str1, k);
    return 0;
}

0
投票
 def check(_str):
    length = len(_str)
    for i in range(length):
        if (_str[i]!=_str[(input2+i)%length]):
            return 0
    return 1
words = input1.split()
ans = list(map(check,words))
print(sum(ans))

code in Python


0
投票
package fun;

public class Test1 {

    public static void main(String[] args) {
        System.out.println(rotatedWords("llohe ereth", 2));
        System.out.println(rotatedWords("adaada", 3));
    }

    public static int rotatedWords(String input1, int input2) {
        int count = 0;
        String arr[] = input1.split(" ");
        if (input1 != null && !input1.isEmpty()) {
            for (int i = 0; i < arr.length; i++) {
                System.out.println("Orginal Word :" + arr[i]);
                int start = arr[i].length() - input2;
                String s1 = arr[i].substring(start);
                System.out.println("s1 : " + s1);
                String s2 = arr[i].substring(0, start);
                System.out.println("s2 : " + s2);
                System.out.println("New Word : " + s1 + s2);
                if ((s1 + s2).equals(arr[i])) {
                    count++;
                }
            }
        }
        return count;
    }

}

0
投票
$str = "Hello Friend";
$kTime = 5;
$arr = explode (' ',$str);
$sizeOfArr = count($arr);
$char = array();
$count = 0;
foreach($arr as $k=>$v)
{
  $ln = $kTime%strlen($v);
  $char = substr($v,$ln).substr($v,0,$ln);
  if($v == $char)
    {
      $count++;
    }
}
echo 'Number of Original word after rotation found : '.$count;

0
投票

这是 Python 3 中正确移动的解决方案

def rotatedWords(cls, input1,input2):
    rotate_string = input1.split(' ') # converting string to list
    count = 0
    for i in range(1,input2+1):
        for j in range(len(rotate_string)):
            rotate_string[j]= rotate_string[j][-1]+rotate_string[j][0:-1]
        
    actual_word = input1.split(' ')

    for i in range(len(rotate_string)):
        for j in range(len(actual_word)):
            if rotate_string[i]==actual_word[j]:
                count+=1
    return count

print(rotatedWords(1,"adaada",3))
print(rotatedWords(1,"llohe ereth",2))

0
投票
package com.code;
public class LoveLetter {
public static void main(String args[])
{
    String str="llohe ereth";
    int n=2;
    solution(str,n);
}
public static void solution(String str,int n)
{
    int l=str.length();
    String a[]=str.split("\\s+");
    String str1="";
    int c=0;
    for(int i=0;i<a.length;i++)
    {
        String s=a[i];
        String k=rightrotate(s,n);
        if(a[i].equals(k)){
            c++;
        }
        else
            continue;
    }
    System.out.println(c);
}
static String rightrotate(String str, int d)
{
    d=str.length()-d;
    String ans = str.substring(d) + str.substring(0, d);
    return ans;
}
}

0
投票

假设有人将每个单词的字符从左到右旋转K次。因此,如果我们想知道原始单词,我们必须将每个字符从右到左旋转K次

def rotatedWords(cls, input1, input2):
    original_string = input1.split(' ')  # the string is split into list
    count = 0  # for counting the words that remains same even after rotation

    for i in range(0, input2):
        for j in range(len(original_string)):
            original_string[j] = original_string[j][-1]+original_string[j][0:-1]  # taking the last character and all characters from first to second last of each word,then concatenating them

    given_string = input1.split(' ')

    for i in range(len(given_string)):
        if original_string[i] == given_string[i]:   #comparing each words from both lists
            count += 1
    return count

print(rotatedWords(1,"adaada", 3))
print(rotatedWords(1,"llohe ereth", 2))

0
投票

问题陈述表明单词之间的空格可以是“一个或多个”。 因此,如果 split 删除所有空格,您将通过所有测试用例。 请改用 input1.split("\\s+") 正则表达式。

    


-1
投票

Java解决方案* import java.util.Scanner; public class Main { //rotate every word of the given string by the given rotation value public static String rotate_string (String str, int d) { String ans = str.substring (d) + str.substring (0, d); return ans; } public static void rotated (String str, int rotated) { String[]wordsarr = str.split ("\\s"); // convert string to an array of strings. String rotatedarr[] = new String[wordsarr.length]; // to store the rotated array. for (int i = 0; i < wordsarr.length; i++) { rotatedarr[i] = rotate_string(wordsarr[i], wordsarr[i].length () - rotated); } int count = 0; for (int i = 0; i < wordsarr.length; i++) { if (wordsarr[i].equals (rotatedarr[i])) { count++; } } System.out.println (count); } public static void main (String[]args) { Scanner s= new Scanner(System.in); String str= s.nextLine(); //input string int rotate_by= s.nextInt(); rotated(str,rotate_by); } }

© www.soinside.com 2019 - 2024. All rights reserved.