package com.sunny.grokkingalgorithms.basic_2020.warmup;

public class PasswordDecryption {
	
	/*
	 * 
	 */
	 public static String decryptPassword(String s) {
		    // Write your code here
		 int i = 0;
		 char[] decrypted = new char[calculatedLength(s)];
		 int j  = 0;
		 while(i < s.length()) {
			char current = s.charAt(i);
			if(Character.isDigit(current)) {
				int index = findZero(decrypted);
				decrypted[index] = current;
			}
			else if(Character.isUpperCase(current) && 
					(i+1 < s.length() && 
							Character.isLowerCase(s.charAt(i+1)))) {
				decrypted[j] = s.charAt(i+1);
				decrypted[j+1] = current;
			}else {
			    decrypted[j] = current;	
			}			
			j++;
			i++;
		 }
		 return String.valueOf(decrypted);
	 }
	 
	 public static int findZero(char[] dec) {
		 int index = -1;
		 for(int i = 0 ; i < dec.length ; i++) {
			 if(dec[i] == '0') {
				 index = i;
				 break;
			 }
		 }
		 return index;
	 }
	 
	 
	 public static int calculatedLength(String s) {
		 int len = 0;
		 for(int i = 0 ; i < s.length() ; i++) {
			 int cur = s.charAt(i);
			 if(cur != '*') {
				 len++;				 
			 }
		 }
		 return len;
	 }
	 
	 

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String input = "51Pa*0Lp*0e";
		System.out.println(decryptPassword(input));
	}

}

@@ -198,22 +198,29 @@
 = 0;%0A%09%09 
-char%5B%5D
+StringBuilder
  decrypt
@@ -232,48 +232,61 @@
 new 
-char%5BcalculatedLength(s)%5D;%0A%09%09 int j  = 0
+StringBuilder();%0A%09%09 char%5B%5D original = s.toCharArray()
 ;%0A%09%09
@@ -300,25 +300,31 @@
 i %3C 
-s
+original
 .length
-()
 ) %7B%0A%09%09%09
+ 
 char
@@ -334,32 +334,33 @@
 rrent = 
-s.charAt(i)
+original%5Bi%5D
 ;%0A%09%09%09
+ 
 if(Chara
@@ -388,16 +388,17 @@
 ) %7B%0A%09%09%09%09
+ 
 int inde
@@ -414,60 +414,112 @@
 ero(
-decrypted);%0A%09%09%09%09decrypted%5Bindex%5D = current;%0A%09%09%09
+original,i);%0A%09%09%09%09 if(index %3E 0) %7B%0A%09%09%09%09%09 original%5Bindex%5D = current;%0A%09%09%09%09%09 original%5Bi%5D = '-';%0A%09%09%09%09 
 %7D%0A%09%09%09
+ %7D
 else
@@ -556,47 +556,51 @@
 ent)
- && 
 %0A%09%09%09%09%09
+ && 
 (i+1 %3C 
-s
+original
 .length
-()
  &&
- 
 %0A%09%09%09%09%09%09%09
 Char
@@ -595,16 +595,17 @@
 %0A%09%09%09%09%09%09%09
+ 
 Characte
@@ -606,16 +606,25 @@
 aracter.
+%0A%09%09%09%09%09%09%09 
 isLowerC
@@ -627,29 +627,29 @@
 werCase(
-s.charAt(i+1)
+original%5Bi+1%5D
 ))) %7B%0A%09%09
@@ -654,174 +654,301 @@
 %09%09%09%09
-decrypted%5Bj%5D = s.charAt(i+1)
+ char temp = original%5Bi+1%5D
 ;%0A%09%09%09%09
-decrypted%5Bj+1%5D = current;%0A%09%09%09%7Delse %7B%0A%09%09%09    decrypted%5Bj%5D = current;%09%0A%09%09%09%7D%09%09%09%0A%09%09%09j++;%0A%09%09%09i++;%0A%09%09 %7D%0A%09%09 return String.valueOf(decrypted
+ original%5Bi+1%5D = original%5Bi%5D;%0A%09%09%09%09 original%5Bi%5D = temp;%0A%09%09%09 %7D%0A%09%09%09 i++;%09%09%09 %0A%09%09 %7D%0A%09%09 for(int j = 0 ; j %3C original.length;j++) %7B%0A%09%09%09 char cur = original%5Bj%5D;%0A%09%09%09 if(cur != '-' && cur != '*') %7B%0A%09%09%09%09 decrypted.append(cur);%09%09%09%09 %0A%09%09%09 %7D%0A%09%09 %7D%0A%09%09 return decrypted.toString(
 );%0A%09
@@ -992,16 +992,27 @@
 ar%5B%5D dec
+, int start
 ) %7B%0A%09%09 i
@@ -1037,25 +1037,29 @@
 for(int i = 
-0
+start
  ; i %3C dec.l

Completing the decryption problem