Skip to content
Home Β» String Copy

String Copy


πŸ“˜ String Copy (Under String Operations in Data Structures)

String Copy is a basic string operation where the contents of one string are duplicated into another string.

Example:

Source: "HELLO"
Destination: "HELLO"

In Data Structures, strings behave like arrays of characters, so copying involves copying each character sequentially.


πŸ“Œ Why String Copy Matters in DSA?

  • Strings are stored as sequences in memory
  • Copying determines space and time complexity
  • Strings are immutable in Python β†’ you can’t modify them directly
  • Many algorithms use copy operations internally:
    • Substring creation
    • Concatenation
    • Dynamic resizing
    • Buffer operations

πŸ“˜ What Happens During String Copy? (Conceptual)

For a string of length n, copying involves:

  1. Allocating memory for the new string
  2. Copying each character one by one
  3. Adding a null terminator (\0) in languages like C
  4. Returning the address of the new string

Time Complexity

[
O(n)
]

Because each of the n characters must be copied.


πŸ“Œ String Copy in Python

Python strings are immutable, so copying does not copy character-by-character unless necessary.

Case 1: Direct Assignment

a = "hello"
b = a

βœ” No new copy
βœ” Both refer to the same string object
βœ” O(1) time

This is reference copying, not content copying.


Case 2: Full Content Copy (actual cloning)

a = "hello"
b = a[:]
c = str(a)
d = "".join(a)

βœ” Creates a new string object
βœ” Time complexity = O(n)


πŸ“˜ Python Examples of String Copy

βœ” Simple copy using slicing

s1 = "DataStructure"
s2 = s1[:]      # full copy
print(s2)

βœ” Using str()

s1 = "Python"
s2 = str(s1)    # creates a new copy

βœ” Manual character-by-character copy (DSA-style)

s1 = "HELLO"
s2 = ""

for ch in s1:
    s2 += ch     # O(nΒ²) inefficient

Why O(nΒ²)?

  • Because each += creates a new string β†’ copying repeatedly.

Better approach:

s1 = "HELLO"
temp = []

for ch in s1:
    temp.append(ch)

s2 = "".join(temp)   # O(n)

πŸ“˜ String Copy in Memory (DSA View)

Consider:

char s1[] = "ABC";
char s2[4];

Copy steps:

  • s2[0] = s1[0] β†’ ‘A’
  • s2[1] = s1[1] β†’ ‘B’
  • s2[2] = s1[2] β†’ ‘C’
  • s2[3] = ‘\0’

Same idea applies conceptually, even though Python abstracts it.


πŸ“Œ Applications of String Copy

  • Creating new buffers
  • Returning substrings
  • Editing strings in memory
  • String manipulation algorithms:
    • pattern matching
    • string reverse
    • concatenation
    • encryption/encoding

πŸ“˜ Efficiency Concerns

MethodDescriptionTime
Direct assignmentNo new copyO(1)
Slicing ([:])Full copyO(n)
str()Full copyO(n)
"".join(list)Efficient buildingO(n)
+= in loopSlow repeated copyingO(nΒ²)

For large strings, always avoid += inside loops.


πŸ“˜ Summary (Exam Notes)

  • String Copy = duplication of one string into another
  • Conceptually: copy characters sequentially
  • Time complexity = O(n)
  • Python strings are immutable, assignment only copies reference
  • Full copies use slicing, join, or str()
  • Avoid repeated concatenations β†’ expensive O(nΒ²)