stumped on a Java interview, need some hints

后端 未结 8 1906
天涯浪人
天涯浪人 2021-01-30 18:00

This is an interview problem that I am stuck on:

Given a string consisting of a, b and c\'s, we can perform the following operation: Take any two adjacent

8条回答
  •  故里飘歌
    2021-01-30 18:50

    This is one of the sample questions on InteviewStreet. I tried solving it in Java and my code produces the expected results for the three test cases given, but then only passes one of the ten actual test cases. I'm a lot rusty in programming and in Java but this is what I came up with to solve the three given examples...

    I can't get the code formatting just right...

    import java.io.*;
    import java.util.Scanner;
    public class Solution
    {
        public String transform(String input)
        {
            String output = new String(input);
            // System.out.println(input);
            if(output.length() > 1)
            {
                int i = 0;
                while (i < (output.length() - 1))
                {
                    if(output.charAt(i) != output.charAt(i+1))
                    {
                        StringBuffer sb = new StringBuffer();
                        if (output.charAt(i) == 'a')
                        {
                            // next character is b or c
                            if (output.charAt(i+1) == 'b')
                            {
                                sb.append('c');
                            }
                            else
                            {
                                sb.append('b');
                            }
                        }
                        else if (output.charAt(i) == 'b')
                        {
                            // next character is a or c
                            if (output.charAt(i+1) == 'a')
                            {
                                sb.append('c');
                            }
                            else
                            {
                                sb.append('a');
                            }
                        }
                        else
                        {
                            // next character is a or b
                            if (output.charAt(i+1) == 'a')
                            {
                                sb.append('b');
                            }
                            else
                            {
                                sb.append('a');
                            }
                        }
                        String remainder = output.substring(i+2);
                        if (remainder.length() > 0)
                        {
                            sb.append(remainder);
                        }
                        Solution anotherAnswer = new Solution();
                        output = anotherAnswer.transform(sb.toString());
    
                    } 
                    i++;
                }
            }
    
            return output;
        }
    
    
        /**
         * @param args
         * @throws Exception 
         */
        public static void main(String[] args) throws Exception
        {
            // TODO Auto-generated method stub
            Scanner sc;
            try
            {
                sc = new Scanner(new BufferedReader(new FileReader("input.txt")));
                // sc = new Scanner(System.in);
                int numberOfTests = sc.nextInt();
                for (int i = 1; i <= numberOfTests; i++)
                {
                    String testData = sc.next();
                    Solution answer = new Solution();
                    String transformedData = answer.transform(testData);
                    System.out.println(transformedData.length());
                }
            }
            catch (Exception e) 
            {
                throw new Exception(e);
            }
        }
    
    }
    

提交回复
热议问题