Tuesday, October 30, 2018

Q1: Reverse a string without affecting special characters

Question 1: Reverse a string without affecting special characters
  

Note that $ and , are not moved anywhere. Only sub sequence "abc" is reversed.

Eg 1:
    Input:   str = "a,b$c"
   Output:  str = "c,b$a"
 


Eg 2:
    Input:   str = "Ab,c,de!$"
    Output:  str = "ed,c,bA!$"

1 comment:

  1. Here is the solution. there may be someother easy way too. if someone finds, pls do share here. Also, feel free to share ur comments.
    s = ".,a,eioub$c$@"
    print("actual string: " + s)
    x = [i for i in s[::-1] if i.isalpha()]
    [x.insert(i, j) for i, j in enumerate(s) if not j.isalpha()]
    print("final string: " + "".join(x))

    ReplyDelete