Open In App

Python3 Program for Swap characters in a String

Given a String S of length N, two integers B and C, the task is to traverse characters starting from the beginning, swapping a character with the character after C places from it, i.e. swap characters at position i and (i + C)%N. Repeat this process B times, advancing one position at a time. Your task is to find the final String after B swaps.

Examples: 



Input : S = "ABCDEFGH", B = 4, C = 3;
Output:  DEFGBCAH
Explanation:
         after 1st swap: DBCAEFGH
         after 2nd swap: DECABFGH
         after 3rd swap: DEFABCGH
         after 4th swap: DEFGBCAH

Input : S = "ABCDE", B = 10, C = 6;
Output : ADEBC
Explanation:
         after 1st swap: BACDE
         after 2nd swap: BCADE
         after 3rd swap: BCDAE
         after 4th swap: BCDEA
         after 5th swap: ACDEB
         after 6th swap: CADEB
         after 7th swap: CDAEB
         after 8th swap: CDEAB
         after 9th swap: CDEBA
         after 10th swap: ADEBC

Naive Approach

Below is the implementation of the approach:






# Python Program to Swap characters in a String
 
def swapCharacters(s, B, C):
    N = len(s)
    # If c is greater than n
    C = C % N
     
    # Converting string to list
    s = list(s)
     
    # loop to swap ith element with (i + C) % n th element
    for i in range(B):
        s[i], s[(i + C) % N] = s[(i + C) % N], s[i]
    s = ''.join(s)
    return s
 
# Driver program
s = "ABCDEFGH"
B = 4
C = 3
print(swapCharacters(s, B, C))
 
# This code is contributed by Susobhan Akhuli

Output
DEFGBCAH

Time Complexity: O(B), to iterate B times.
Auxiliary Space: O(1)

Efficient Approach: 

Example:
s = ABCDEFGHIJK; c = 4;
parts: ABCD EFGHIJK
after 1 full iteration: DABC IJKEFGH 
after 2 full iteration: CDAB FGHIJKE 
after 3 full iteration: BCDA JKEFGHI 
after 4 full iteration: ABCD GHIJKEF 
after 5 full iteration: DABC KEFGHIJ 
after 6 full iteration: CDAB HIJKEFG 
after 7 full iteration: BCDA EFGHIJK 
after 8 full iteration: ABCD IJKEFGH 
 

Below is the implementation of the approach:




# Python3 program to find new after swapping
# characters at position i and i + c
# b times, each time advancing one
# position ahead
 
# Method to find the required string
def swapChars(s, c, b):
     
    # Get string length
    n = len(s)
     
    # If c is larger or equal to the length of
    # the string is effectively the remainder of
    # c divided by the length of the string
    c = c % n
     
    if (c == 0):
         
        # No change will happen
        return s
         
    f = int(b / n)
    r = b % n
     
    # Rotate first c characters by (n % c)
    # places f times
    p1 = rotateLeft(s[0 : c], ((c * f) % (n - c)))
     
    # Rotate remaining character by
    # (n * f) places
    p2 = rotateLeft(s[c:], ((c * f) % (n - c)))
     
    # Concatenate the two parts and convert the
    # resultant string formed after f full
    # iterations to a character array
    # (for final swaps)
    a = p1 + p2
    a = list(a)
     
    # Remaining swaps
    for i in range(r):
         
        # Swap ith character with
        # (i + c)th character
        temp = a[i]
        a[i] = a[(i + c) % n]
        a[(i + c) % n] = temp
 
    # Return final string
    return str("".join(a))
 
def rotateLeft(s, p):
     
    # Rotating a string p times left is
    # effectively cutting the first p
    # characters and placing them at the end
    return s[p:] + s[0 : p]
 
# Driver code
 
# Given values
s1 = "ABCDEFGHIJK"
b = 1000
c = 3
 
# Get final string
s2 = swapChars(s1, c, b)
 
# Print final string
print(s2)
 
# This code is contributed by avanitrachhadiya2155

Output: 
CADEFGHIJKB

 

Time Complexity: O(n) 
Space Complexity: O(n)
 

Please refer complete article on Swap characters in a String for more details!


Article Tags :