• + 6 comments

    Try to solve in Java. Calculating all next lexicographical permutation using this logic. But Test#1, #2 says 4s Terminated due to time out. Any suggestion?

    • + 5 comments

      test the case when input is just one letter (e.g. p), the output should be "no answer". This solved my problem.

      • + 2 comments

        no it didn't solved

        • + 0 comments

          first of all check if same letters are repeating

        • + 2 comments

          int main() {

          int t,i;
          
          cin >> t;
          
          string s;
          
          for(i=0;i<t;i++)
          {
          
           cin >> s;
          
          bool val = next_permutation(s.begin(), s.end());
          
          if (val == false)
          {
          
              cout << "no answer" << endl;
          
          }
          
          else
          
          {
          
              cout << s << endl;
          
          }
          

          }

           return 0;
          

          }

          • + 0 comments

            Possible implementation from cppreference.com

            template<class BidirIt>
            bool next_permutation(BidirIt first, BidirIt last)
            {
                if (first == last) return false;
                BidirIt i = last;
                if (first == --i) return false;
             
                while (true) {
                    BidirIt i1, i2;
             
                    i1 = i;
                    if (*--i < *i1) {
                        i2 = last;
                        while (!(*i < *--i2))
                            ;
                        std::iter_swap(i, i2);
                        std::reverse(i1, last);
                        return true;
                    }
                    if (i == first) {
                        std::reverse(first, last);
                        return false;
                    }
                }
            }
            
      • + 0 comments

        thanks, it solved my problem.

      • + 0 comments

        Thanks. It helped to pass test cases #1 and #2

      • + 0 comments

        such a relief!!!

    • + 3 comments

      Try to use

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      

      to read the input. It has 100000 test cases so Scanner won't work

      • + 1 comment

        can you please tell how to BufferReader to scan multiple lines of Strings with Exception Handling

        • + 3 comments

          here is what i did:

          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          int num = Integer.parseInt(in.readLine());
          String line = "";
          for (int i = 0; i < num; i++) {
          	line = in.readLine();
          	// Do your stuff here
          }
          
          • + 0 comments

            Thanks buddy

          • + 1 comment

            didn't work, i still get timeouts

            • + 1 comment

              I did this.. Lengthy but worked.

              import java.util.*;
              class Solution {
              	public static void main(String[] args) {
              		Scanner sc=new Scanner(System.in);
              		int t=sc.nextInt();
              		for(int test=1;test<=t;test++) {
              			String s=sc.next(),a="";
              			int numberCharsTaken;
              			int flag = 0,x=0;
              			for(numberCharsTaken = 1;numberCharsTaken<=s.length();numberCharsTaken++) {
              				a = s.substring(s.length() - numberCharsTaken);
              				for(x = a.length()-1;x>0;x--) {
              					if(a.charAt(0)<a.charAt(x)) {
              						flag = 1;
              						break;
              					}
              					if(flag == 1)
              						break;
              				}
              				if(flag == 1)
              					break;
              			}
              			if(flag == 1){
              				String temp = a.substring(0,x)+a.substring(x+1);
              				char c[] = temp.toCharArray();
              				Arrays.sort(c);
              				temp = a.charAt(x) + new String(c);
              				String ans = s.substring(0,s.length()-numberCharsTaken)+temp;
              				System.out.println(ans);	
              			}
              			else
              				System.out.println("no answer");
              		}
              	}
              }
              
              • + 1 comment

                @shraiysh Can you explain the logic here. The lexicographical permutation solution didnt work for me. It is giving me runtime error. But your code is working for me. I dont understand why.

                • + 0 comments

                  For me it threw a runtime error when I was not concidereing w.length = 1 cases.

          • + 0 comments

            It did not work. It stuck in line 20348

      • + 0 comments

        Scanner will work in these cases. I submitted using scanner. The problem must lie elsewhere.

      • + 0 comments

        thanks! that is what I needed. Don't really know why Scanner is slower than buffreader though.

    • + 1 comment

      Simple java solution : algorithm n wikipedia

      static String biggerIsGreater(String w) { char[] arr = w.toCharArray(); int i = arr.length - 1; // finding p --> i while(i>0 && arr[i-1]>=arr[i]){ i--; } if(i<=0){ //System.out.println("Pretty much last one!!"); return "no answer"; } int j = arr.length-1; while(arr[j]<= arr[i-1]){ j--; } char temp = arr[i-1]; arr[i-1] = arr[j]; arr[j] = temp;

          j = arr.length - 1;
          while(i<j)
          {
              char tem = arr[i];
              arr[i] = arr[j];
              arr[j] = tem;
              j--;
              i++;
          }
          String ret = new String(arr);
          return ret;
      }
      
      • + 1 comment

        this logic is to rev the string in O(n) time and O(1) space. can u explain, how this iis working?

        • + 0 comments

          its actually n/2

    • + 0 comments

      Use this to find no answer without solving:

      https://www.hackerrank.com/challenges/bigger-is-greater/forum/comments/595411

    • + 0 comments

      I am having the same TLE problem in test #1 and #2. Can someone help me out?