Làm cách nào để loại bỏ các bản sao trong chuỗi trong Python bằng đệ quy?

Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách xóa các ký tự trùng lặp khỏi một chuỗi đã cho bằng chương trình Java. Chúng ta sẽ học nó bằng phương pháp đệ quy

Hiểu vấn đề

Trong bài toán này, chúng ta sẽ là một Chuỗi chứa các ký tự trùng nhau liên tiếp Ví dụ. aabbbcddd chúng ta phải trả về kết quả là abcd. Chuỗi là tập hợp các ký tự nên trong bài toán này chúng ta sẽ kiểm tra tất cả các ký tự trong chuỗi đã cho và sau đó chúng ta sẽ loại bỏ tất cả các ký tự trùng lặp liên tiếp rồi trả về chuỗi

Đôi khi đệ quy có thể là một chút khó khăn. Đệ quy là một phương pháp giải một bài toán cụ thể trong đó chúng ta tính toán nghiệm chỉ bằng cách sử dụng một trường hợp nhỏ hơn của cùng một bài toán. Trong lập trình, đệ quy sử dụng một hàm gọi chính nó một cách trực tiếp hoặc gián tiếp và hàm tương ứng đó được gọi là hàm đệ quy

Chương trình tìm hiểu Nhân đôi liên tiếp

import java.util.Scanner;
class duplicateChar {
  public static String removeCosecutiveDuplicates[String s] {
    if[s.length[] 0 and len[s] > 0 and remStr[0] == s[len[s] - 1]:

                while len[remStr] > 0 and remStr[0] != ch and remStr[0] == s[len[s] - 1]:
                    remStr = remStr[1: len[remStr]]

                s = s[0: len[s] - 1]

            s = s + remStr
            i = j

        else:
            i += 1

    return s


str1 = "mississipie"
print[removeDuplicates[str1, ' ']]

Đưa ra một chuỗi, loại bỏ đệ quy các ký tự trùng lặp liền kề khỏi chuỗi. Chuỗi đầu ra không được có bất kỳ bản sao liền kề nào. Xem các ví dụ sau

ví dụ.  

Đầu vào. azzzzy
đầu ra. phải
Đầu tiên “azxxzy” được rút gọn thành “azzy”.  
Chuỗi “azzy” chứa các bản sao,
vì vậy nó được rút gọn thành “ay”

Đầu vào. chuyên viên máy tính
đầu ra. gksfor
"Geeksforgeeg" đầu tiên được giảm xuống
“gksforgg”. Chuỗi “gksforgg”
chứa các bản sao, vì vậy nó là hơn nữa
giảm xuống "gksfor"

Đầu vào. caaaabbbaacdddd
đầu ra. Chuỗi rỗng

Đầu vào. acaaaabbbacdddd
đầu ra. được

Khuyến khích. Vui lòng giải quyết nó trên “PRACTICE” trước khi chuyển sang giải pháp.  
 

Có thể thực hiện theo cách tiếp cận sau để loại bỏ các bản sao trong thời gian O[N].  

  • Bắt đầu từ ký tự ngoài cùng bên trái và xóa các ký tự trùng lặp ở góc bên trái nếu có
  • Ký tự đầu tiên phải khác với ký tự liền kề của nó bây giờ. Đệ quy cho chuỗi có độ dài n-1 [chuỗi không có ký tự đầu tiên]
  • Gọi xâu thu được sau khi rút gọn xâu con bên phải độ dài n-1 là rem_str. Có ba trường hợp có thể xảy ra
    1. Nếu ký tự đầu tiên của rem_str khớp với ký tự đầu tiên của chuỗi gốc, hãy xóa ký tự đầu tiên khỏi rem_str
    2. Nếu chuỗi còn lại trống và ký tự bị xóa cuối cùng giống với ký tự đầu tiên của chuỗi gốc. Trả về chuỗi rỗng
    3. Khác, nối thêm ký tự đầu tiên của chuỗi gốc vào đầu rem_str
  • Trả lại rem_str

Hình ảnh dưới đây là một cách khô khan của phương pháp trên

Dưới đây là việc thực hiện các phương pháp trên

con trăn




# Python program to remove all 

# adjacent duplicates from a string

 

# Recursively removes adjacent 

# duplicates from str and returns

________số 8_______

# pointer to last_removed character

def removeUtil[string, last_removed]:

 

# Python program to remove all 1____3_______2

# Python program to remove all 1_______3_______4 # Python program to remove all 5# Python program to remove all 6_______3_______7# Python program to remove all 7 # Python program to remove all 9 # adjacent duplicates from a string0 # Python program to remove all 5# Python program to remove all 6# Python program to remove all 7# Python program to remove all 7 # adjacent duplicates from a string5# adjacent duplicates from a string6

# adjacent duplicates from a string7_______4_______8 # adjacent duplicates from a string9

 

# Python program to remove all 1_______5_______2

# Python program to remove all 1_______5_______4

# Python program to remove all 1_______5_______6

# Python program to remove all 1_______3_______4  9# Python program to remove all 9_______6_______1# Python program to remove all 7# Python program to remove all 7  9# adjacent duplicates from a string5# Recursively removes adjacent 6

# adjacent duplicates from a string7_______6_______8# Python program to remove all 7 # duplicates from str and returns0# duplicates from str and returns1# Python program to remove all 9# duplicates from str and returns3

# adjacent duplicates from a string7_______7_______5 # Python program to remove all 5# duplicates from str and returns7_______4_______5 # duplicates from str and returns9

# new string. las_removed is a 0_______5_______9# Python program to remove all 9_______6_______1_______3_______7# Python program to remove all 7  9# adjacent duplicates from a string5# Recursively removes adjacent 6

# new string. las_removed is a 9# adjacent duplicates from a string9# Python program to remove all 7  9# adjacent duplicates from a string5# pointer to last_removed character4

# adjacent duplicates from a string7_______4_______9# Python program to remove all 7  9# adjacent duplicates from a string5# pointer to last_removed character4

 

# adjacent duplicates from a string7_______4_______8 def4

 

# Python program to remove all 1_______10_______7

# Python program to remove all 1_______10_______9

# Python program to remove all 1_______11_______1

# Python program to remove all 1_______11_______3

# Python program to remove all 1____11_______5

# Python program to remove all 1_______11_______7# Python program to remove all 7 removeUtil[string, last_removed]:9_______4_______5# Python program to remove all 01

# Python program to remove all 02______3_______03

 

# Python program to remove all 1____3_______06

# Python program to remove all 1____3_______08

# Python program to remove all 1______3_______10

# Python program to remove all 1_______3_______12

# Python program to remove all 1_______3_______4 # Python program to remove all 5# Python program to remove all 16_______3_______7 # Python program to remove all 9 # duplicates from str and returns9

# Python program to remove all 20_______3_______21# Python program to remove all 9# Recursively removes adjacent 1_______3_______7# Python program to remove all 7  9# Python program to remove all 9# Recursively removes adjacent 6

# adjacent duplicates from a string7_______6_______8# Python program to remove all 7 # duplicates from str and returns0# duplicates from str and returns1# Python program to remove all 9# duplicates from str and returns3

# adjacent duplicates from a string7_______4_______8 # Python program to remove all 38_______4_______5_______3_______40

 

# Python program to remove all 1_______3_______43

# Python program to remove all 1_______3_______45

# Python program to remove all 1______3_______47

# Python program to remove all 1_______3_______49

# Python program to remove all 1____3_______51

# Python program to remove all 1_______3_______4 # Python program to remove all 5# Python program to remove all 55# Python program to remove all 7# Python program to remove all 7 # Python program to remove all 9 # duplicates from str and returns9

# Python program to remove all 60# Recursively removes adjacent 8# Python program to remove all 7_______3_______7 # duplicates from str and returns0# duplicates from str and returns1# Python program to remove all 9# Python program to remove all 67

# adjacent duplicates from a string7_______4_______8 removeUtil[string, last_removed]:7

 

# Python program to remove all 1______3_______73

# Python program to remove all 1______3_______75

# Python program to remove all 1______3_______77

# Python program to remove all 1______3_______79

# Python program to remove all 1_______3_______81

# Python program to remove all 1_______4_______8 # Python program to remove all 84_______3_______9# Python program to remove all 86# Python program to remove all 87 # Python program to remove all 88

 

def # Python program to remove all 91

# Python program to remove all 1_______6_______8# Python program to remove all 7 # Python program to remove all 9

# Python program to remove all 1_______4_______8 # Python program to remove all 98

# Python program to remove all 99# adjacent duplicates from a string00

 

# adjacent duplicates from a string02

def # adjacent duplicates from a string04

# Python program to remove all 1_______4_______06____3_______7 # adjacent duplicates from a string08

# Python program to remove all 1_______4_______10 # adjacent duplicates from a string11# adjacent duplicates from a string12 # adjacent duplicates from a string13

# adjacent duplicates from a string7_______4_______15

# Python program to remove all 1_______4_______8 # adjacent duplicates from a string06

 

def # adjacent duplicates from a string21

# Python program to remove all 1_______4_______8 # adjacent duplicates from a string24

 

# adjacent duplicates from a string26

# adjacent duplicates from a string27______3_______7 # adjacent duplicates from a string29

# adjacent duplicates from a string30 # adjacent duplicates from a string31

 

# adjacent duplicates from a string33_______3_______7 # adjacent duplicates from a string35

# adjacent duplicates from a string30 # adjacent duplicates from a string37

 

# adjacent duplicates from a string39# Python program to remove all 7 # adjacent duplicates from a string41

# adjacent duplicates from a string30 # adjacent duplicates from a string43

 

# adjacent duplicates from a string45______3_______7 # adjacent duplicates from a string47

# adjacent duplicates from a string30 # adjacent duplicates from a string49

 

# adjacent duplicates from a string51______3_______7 # adjacent duplicates from a string53

# adjacent duplicates from a string30 # adjacent duplicates from a string55

 

# adjacent duplicates from a string57# Python program to remove all 7 # adjacent duplicates from a string59

# adjacent duplicates from a string30 # adjacent duplicates from a string61

 

# adjacent duplicates from a string63_______3_______7 # adjacent duplicates from a string65

# adjacent duplicates from a string30 # adjacent duplicates from a string67

 

# adjacent duplicates from a string69______3_______7 # adjacent duplicates from a string71

# adjacent duplicates from a string30 # adjacent duplicates from a string73

 

# adjacent duplicates from a string75# Python program to remove all 7 # adjacent duplicates from a string77

# adjacent duplicates from a string30 # adjacent duplicates from a string79

# adjacent duplicates from a string80

đầu ra.  

gksfor
ay
g
a
qrq
acac
a

Thời gian phức tạp. Độ phức tạp thời gian của giải pháp có thể được viết là T[n] = T[n-k] + O[k] trong đó n là độ dài của chuỗi đầu vào và k là số ký tự đầu tiên giống nhau. Giải pháp của sự truy hồi là O[n]

Chủ Đề