function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fiona gentryfiona gentry 

How to Return an array of characters in reversed order for alphanumeric characters only however all other non-alphanumeric characters need to retain their original position

Dear gurus,

I have a problem where 

GIVEN there is a string of characters, and i need to ivoke a reverse method is invoked such that it returns an array of characters in reversed order for alphanumeric characters only however all other non-alphanumeric characters need to retain their original position -
How to Implement solution in Apex?

I tried below code but no luck
public class assessment {
  public static List<String> reverseSpecialString(String input) {
        List<String> resultList = new List<String>();
         // IMPLEMENT
        resultList = input.split('');
        string reversedString='';
        for(integer i=resultList.size()-1;i>=0;i--)
            {
             
            reversedString=reversedString+resultList[i];
               
            }
    
       
        
        
         return resultList;
      
       }
    
    public static void validateSolution() {
        String input = 'ABC@HI#J2';
        List<String> expected = new List<String> {'2','J','I','@','H','C','#','B','A'};
        List<String> actual = reverseSpecialString(input);
        
        System.assertEquals(expected, actual, 'Invalid Results');
    }
}

Thanks,
Fiona​​​​​​​

 
Best Answer chosen by fiona gentry
Surya GSurya G
Hi Fiona,
we can use isAlphaNumeric() method to check the string characters and reverse it , and add the nonalphanumeric values at the exact index position using map.
public class assessment {
  public static List<String> reverseSpecialString(String input) {
    List<String> inputList = input.split('');
    Map<Integer,String> stringMap = new Map<Integer,String>();
    List<String> output = new List<String>();
    for (Integer i=0; i<inputList.size(); i++) {
        String charac = inputList[i];
        if(!charac.isAlphaNumeric()) {
            stringMap.put(i,charac);
        }else {
            output.add(charac);
        }
    }
    String finalString =  String.join(output,'');
    List<String> resultList = finalString.reverse().split('');
    for( Integer I : stringMap.keySet() ){
        system.debug(I);
        resultList.add(I,stringMap.get(I));
	}
      return resultList;
  }  
}
Let me know if that helps.

Thanks
Surya G

 

All Answers

Surya GSurya G
Hi Fiona,
we can use isAlphaNumeric() method to check the string characters and reverse it , and add the nonalphanumeric values at the exact index position using map.
public class assessment {
  public static List<String> reverseSpecialString(String input) {
    List<String> inputList = input.split('');
    Map<Integer,String> stringMap = new Map<Integer,String>();
    List<String> output = new List<String>();
    for (Integer i=0; i<inputList.size(); i++) {
        String charac = inputList[i];
        if(!charac.isAlphaNumeric()) {
            stringMap.put(i,charac);
        }else {
            output.add(charac);
        }
    }
    String finalString =  String.join(output,'');
    List<String> resultList = finalString.reverse().split('');
    for( Integer I : stringMap.keySet() ){
        system.debug(I);
        resultList.add(I,stringMap.get(I));
	}
      return resultList;
  }  
}
Let me know if that helps.

Thanks
Surya G

 
This was selected as the best answer
fiona gentryfiona gentry
@Surya G

what is the signifcance for adding I and stringMap.get(I) in resultList?
resultList.add(I,stringMap.get(I));

 
Surya GSurya G
HI Fiona,

I'm taking the nonalphanumeric values and index as key in the map, so once I did with reversing the alphanumeric string, I add the nonalphanumeric values at their original index using map, where I is the index and stringMap.get(I) gives the nonalphanumeric value at that particular index.