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
- 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
- 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
- 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 string
0 # 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 string
5# adjacent duplicates from a string
6
# adjacent duplicates from a string
7_______4_______8 # adjacent duplicates from a string
9
# 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 string
5# Recursively removes adjacent
6
# adjacent duplicates from a string
7_______6_______8# Python program to remove all
7 # duplicates from str and returns
0# duplicates from str and returns
1# Python program to remove all
9# duplicates from str and returns
3
# adjacent duplicates from a string
7_______7_______5 # Python program to remove all
5# duplicates from str and returns
7_______4_______5 # duplicates from str and returns
9
# 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 string
5# Recursively removes adjacent
6
# new string. las_removed is a
9# adjacent duplicates from a string
9# Python program to remove all
7
9# adjacent duplicates from a string
5# pointer to last_removed character
4
# adjacent duplicates from a string
7_______4_______9# Python program to remove all
7
9# adjacent duplicates from a string
5# pointer to last_removed character
4
# adjacent duplicates from a string
7_______4_______8 def
4
# 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 returns
9
# 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 string
7_______6_______8# Python program to remove all
7 # duplicates from str and returns
0# duplicates from str and returns
1# Python program to remove all
9# duplicates from str and returns
3
# adjacent duplicates from a string
7_______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 returns
9
# Python program to remove all
60# Recursively removes adjacent
8# Python program to remove all
7_______3_______7 # duplicates from str and returns
0# duplicates from str and returns
1# Python program to remove all
9# Python program to remove all
67
# adjacent duplicates from a string
7_______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 string
00
# adjacent duplicates from a string
02
def
# adjacent duplicates from a string
04
# Python program to remove all
1_______4_______06____3_______7 # adjacent duplicates from a string
08
# Python program to remove all
1_______4_______10 # adjacent duplicates from a string
11# adjacent duplicates from a string
12 # adjacent duplicates from a string
13
# adjacent duplicates from a string
7_______4_______15
# Python program to remove all
1_______4_______8 # adjacent duplicates from a string
06
def
# adjacent duplicates from a string
21
# Python program to remove all
1_______4_______8 # adjacent duplicates from a string
24
# adjacent duplicates from a string
26
# adjacent duplicates from a string
27______3_______7 # adjacent duplicates from a string
29
# adjacent duplicates from a string
30 # adjacent duplicates from a string
31
# adjacent duplicates from a string
33_______3_______7 # adjacent duplicates from a string
35
# adjacent duplicates from a string
30 # adjacent duplicates from a string
37
# adjacent duplicates from a string
39# Python program to remove all
7 # adjacent duplicates from a string
41
# adjacent duplicates from a string
30 # adjacent duplicates from a string
43
# adjacent duplicates from a string
45______3_______7 # adjacent duplicates from a string
47
# adjacent duplicates from a string
30 # adjacent duplicates from a string
49
# adjacent duplicates from a string
51______3_______7 # adjacent duplicates from a string
53
# adjacent duplicates from a string
30 # adjacent duplicates from a string
55
# adjacent duplicates from a string
57# Python program to remove all
7 # adjacent duplicates from a string
59
# adjacent duplicates from a string
30 # adjacent duplicates from a string
61
# adjacent duplicates from a string
63_______3_______7 # adjacent duplicates from a string
65
# adjacent duplicates from a string
30 # adjacent duplicates from a string
67
# adjacent duplicates from a string
69______3_______7 # adjacent duplicates from a string
71
# adjacent duplicates from a string
30 # adjacent duplicates from a string
73
# adjacent duplicates from a string
75# Python program to remove all
7 # adjacent duplicates from a string
77
# adjacent duplicates from a string
30 # adjacent duplicates from a string
79
# adjacent duplicates from a string
80
đầ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]