Skip to content

Commit 8c53476

Browse files
committed
changes
1 parent 0a30e26 commit 8c53476

9 files changed

+269
-5
lines changed

src/z_lc_anagram.prog.abap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
*&
55
*&---------------------------------------------------------------------*
66
REPORT z_lc_anagram.
7+
*⏱ Time Complexity:
8+
*Let n be the length of the input strings (since both are equal if they proceed past the length check).
9+
*- Main DO loop:
10+
*- Runs n times
11+
*- Each loop:
12+
*- Two READ TABLE operations into a HASHED TABLE → average O(1) each
13+
*- At most one INSERT per character if not already present → also O(1)
14+
*- So, per iteration = constant work → O(1)
15+
*✅ Total for loop: O(n)
16+
*- Final DELETE and IS INITIAL check:
17+
*- DELETE lt_dict WHERE val = 0 traverses the hash table → at most O(k)
18+
*- k = number of unique characters, and since we're working with single characters, k ≤ 128 (ASCII) → treated as O(1)
19+
*✅ Total Time Complexity: O(n)
20+
*🧠 Space Complexity:
21+
*- lt_dict holds up to k entries for each unique character across both strings
22+
*→ Maximum k = 128 (if extended ASCII, 256; Unicode could be higher, but normally bounded)
23+
*- Regardless of input size, this stays constant for small alphabet sizes
24+
*✅ Total Space Complexity: O(k) → effectively O(1) for ASCI
725

826
TYPES : BEGIN OF ty_dict,
927
let TYPE c,

src/z_lc_binary_tree.prog.abap

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
*&
55
*&---------------------------------------------------------------------*
66
REPORT z_lc_binary_tree.
7+
*⏱️ Time Complexity
8+
*Insertion (add_value):
9+
*- In a perfectly balanced BST, each insertion takes O(log n).
10+
*- But in the worst case—if input is sorted or skewed—it can degrade to a linked list, resulting in O(n) per insertion.
11+
*- With n insertions from the loop, total complexity becomes:
12+
*- Best/Average case: O(n log n)
13+
*- Worst case: O(n²)
14+
*Traversal (print_value):
15+
*- This performs an in-order traversal, visiting each node once → O(n)
16+
*✅ Overall Time Complexity:
17+
*- Best/Average case: O(n log n)
18+
*- Worst case (unbalanced tree): O(n²)
719

820

921
TYPES: BEGIN OF ty_binarybtree,
@@ -51,7 +63,7 @@ FORM add_value USING btree TYPE ty_binarybtree
5163
val TYPE i.
5264

5365
FIELD-SYMBOLS: <lbtree> TYPE ty_binarybtree.
54-
DATA: work TYPE ty_binarybtree.
66+
* DATA: work TYPE ty_binarybtree.
5567

5668
IF btree IS INITIAL.
5769

src/z_lc_compress_string.prog.abap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
*&
55
*&---------------------------------------------------------*
66
REPORT z_lc_compress_string.
7+
*⏱ Time Complexity: O(n)
8+
*- The loop runs once for each character of the input string → DO strlen( p_string ) TIMES → O(n)
9+
*- Each iteration performs only constant-time operations: character access, comparison, and string appending
10+
*- Appending to lv_cstr is handled by ABAP’s string concatenation, which has amortized linear performance, but since we only perform at most n such appends, the overall work remains linear
11+
*✅ Final time complexity: O(n), where n is the length of the input string
12+
*
13+
*🧠 Space Complexity: O(n)
14+
*- lv_cstr stores the compressed result. In the worst case (no repeated characters, like abcdef), the compressed string grows nearly double: 2n
15+
*- Apart from a few scalar variables (lv_curr, lv_next, etc.), there’s no additional memory overhead
16+
*✅ Final space complexity: O(n)
717

818
*aaaabbbcccccdee
919
DATA:lv_cstr TYPE string,

src/z_lc_find_words_from_char.prog.abap

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@
55
*&---------------------------------------------------------------------*
66
REPORT z_lc_find_words_from_char.
77

8+
*⏱ Time Complexity
9+
*Let:
10+
*- n = number of words (lt_words)
11+
*- k = average word length
12+
*- m = number of input characters in lt_chars
13+
*Step-by-step:
14+
*- Initialize lt_char_val with 26 alphabet entries:
15+
*- DO 26 TIMES → O(1)
16+
*- Build frequency table from lt_chars:
17+
*- Loop over m elements → each READ is O(1) in a hashed table
18+
*✅ Total: O(m)
19+
*- Main word loop (LOOP AT lt_words):
20+
*- Runs n times
21+
*- Each iteration:
22+
*- Copies the character frequency table → O(1) (fixed 26 entries)
23+
*- Inner WHILE over each character in the word → O(k)
24+
*- Each READ into lt_char_temp → O(1) ✅ Total: O(n × k)
25+
*Overall:
26+
*Total Time Complexity: O(m + n × k)
27+
*(Most often dominated by n × k if m is small)
28+
29+
*🧠 Space Complexity
30+
*- lt_char_val: Fixed 26-letter hash → O(1)
31+
*- lt_char_temp: Copy of lt_char_val → still O(1)
32+
*- lt_words: Input of n words with k characters each → O(n × k)
33+
*- lt_chars: Input of m characters → O(m)
34+
*Total Space Complexity: O(n × k + m)
35+
*(Driven by input, not auxiliary structures)
36+
837

938

1039
TYPES: tty_words TYPE STANDARD TABLE OF string WITH EMPTY KEY,
@@ -79,3 +108,64 @@ LOOP AT lt_words ASSIGNING FIELD-SYMBOL(<lfs_words>).
79108
WRITE : <lfs_words>.
80109
ENDIF.
81110
ENDLOOP.
111+
*--------------------------------------------------------------------*
112+
113+
*TYPES: BEGIN OF ty_freq,
114+
* char TYPE c,
115+
* val TYPE i,
116+
* END OF ty_freq.
117+
*
118+
*TYPES: tty_words TYPE STANDARD TABLE OF string WITH EMPTY KEY,
119+
* tty_chars TYPE STANDARD TABLE OF c WITH EMPTY KEY.
120+
*
121+
*DATA(lt_words) = VALUE tty_words( ( `coding` ) ( `apple`) ( `google`) ( `sreenith`) ( `pop`) ).
122+
*DATA(lt_chars) = VALUE tty_chars( ( 'a' ) ( 'e') ( 'g') ( 'o') ( 'p') ( 'o') ( 'l') ( 'p') ( 'e') ( 'g') ).
123+
*
124+
*DATA: lt_freq TYPE HASHED TABLE OF ty_freq WITH UNIQUE KEY char,
125+
* ls_freq TYPE ty_freq.
126+
*
127+
*" Initialize frequency table
128+
*LOOP AT lt_chars INTO DATA(ch).
129+
* READ TABLE lt_freq ASSIGNING FIELD-SYMBOL(<fs_freq>) WITH KEY char = ch.
130+
* IF sy-subrc = 0.
131+
* <fs_freq>-val += 1.
132+
* ELSE.
133+
* ls_freq-char = ch.
134+
* ls_freq-val = 1.
135+
* INSERT ls_freq INTO TABLE lt_freq.
136+
* ENDIF.
137+
*ENDLOOP.
138+
*
139+
*" Process each word
140+
*LOOP AT lt_words INTO DATA(word).
141+
* DATA word_freq TYPE HASHED TABLE OF ty_freq WITH UNIQUE KEY char.
142+
* CLEAR word_freq.
143+
*
144+
* DATA is_valid TYPE abap_bool VALUE abap_true.
145+
*
146+
* DO strlen( word ) TIMES.
147+
* DATA(lv_i) = sy-index - 1.
148+
* DATA(lv_char) = word+lv_i(1).
149+
*
150+
* " Count character for this word
151+
* READ TABLE word_freq ASSIGNING FIELD-SYMBOL(<fs_w>) WITH KEY char = lv_char.
152+
* IF sy-subrc = 0.
153+
* <fs_w>-val += 1.
154+
* ELSE.
155+
* ls_freq-char = lv_char.
156+
* ls_freq-val = 1.
157+
* INSERT ls_freq INTO TABLE word_freq.
158+
* ENDIF.
159+
*
160+
* " Compare with available character frequency
161+
* READ TABLE lt_freq INTO DATA(ls_avail) WITH KEY char = lv_char.
162+
* IF sy-subrc <> 0 OR ls_avail-val < word_freq[ KEY char = lv_char ]-val.
163+
* is_valid = abap_false.
164+
* EXIT.
165+
* ENDIF.
166+
* ENDDO.
167+
*
168+
* IF is_valid = abap_true.
169+
* WRITE: / word.
170+
* ENDIF.
171+
*ENDLOOP.

src/zlc_paliandrome_permutations.prog.abap

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
*&
55
*&---------------------------------------------------------------------*
66
REPORT zlc_paliandrome_permutations.
7+
*⏱ Time Complexity
8+
*- Character frequency counting (DO strlen( p_input )):
9+
*- Each iteration: READ and potentially INSERT into a hashed table → O(1) per operation
10+
*- For input string of length n → O(n) total
11+
*- Odd frequency check (LOOP AT lt_freq):
12+
*- At most k iterations, where k is the number of unique characters (bounded, ≤ 128 for ASCII)
13+
*✅ Total Time Complexity: O(n)
14+
15+
*🧠 Space Complexity
16+
*- lt_freq: Stores each unique character and its count → at most O(k) space
17+
*- Scalar variables and field symbols don’t scale with input → negligible
18+
*✅ Total Space Complexity: O(k)
19+
*➡️ With English letters or ASCII input, this is effectively O(1)
20+
721
*Given a string as an input parameter to a program, write code to identify if any permutations of the string is Palindrome or not.
822
*For exmaple:
923
*Given Input: aab
@@ -22,7 +36,7 @@ TYPES: BEGIN OF ty_freq,
2236
count TYPE i,
2337
END OF ty_freq.
2438

25-
DATA: lt_freq TYPE SORTED TABLE OF ty_freq
39+
DATA: lt_freq TYPE HASHED TABLE OF ty_freq
2640
WITH UNIQUE KEY char,
2741
ls_freq TYPE ty_freq.
2842

@@ -34,10 +48,10 @@ DO strlen( p_input ) TIMES.
3448
lv_index = sy-index - 1 .
3549
lv_char = p_input+lv_index(1).
3650

37-
READ TABLE lt_freq INTO ls_freq WITH KEY char = lv_char BINARY SEARCH.
51+
READ TABLE lt_freq ASSIGNING FIELD-SYMBOL(<lfs_freq>) WITH KEY char = lv_char. "BINARY SEARCH.
3852
IF sy-subrc = 0.
39-
ls_freq-count = ls_freq-count + 1.
40-
MODIFY lt_freq FROM ls_freq INDEX sy-tabix.
53+
<lfs_freq>-count = <lfs_freq>-count + 1.
54+
4155
ELSE.
4256
CLEAR ls_freq.
4357
ls_freq-char = lv_char.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*&---------------------------------------------------------------------*
2+
*& Report ZLC_RECURSIVE_TREE_WALKING
3+
*&---------------------------------------------------------------------*
4+
*&
5+
*&---------------------------------------------------------------------*
6+
REPORT ZLC_RECURSIVE_TREE_WALKING.
7+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_PROG" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<PROGDIR>
6+
<NAME>ZLC_RECURSIVE_TREE_WALKING</NAME>
7+
<SUBC>1</SUBC>
8+
<RLOAD>E</RLOAD>
9+
<FIXPT>X</FIXPT>
10+
<UCCHECK>X</UCCHECK>
11+
</PROGDIR>
12+
</asx:values>
13+
</asx:abap>
14+
</abapGit>

src/zlc_sliding_window.prog.abap

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
*&---------------------------------------------------------------------*
2+
*& Report ZLC_SLIDING_WINDOW
3+
*&---------------------------------------------------------------------*
4+
*&
5+
*&---------------------------------------------------------------------*
6+
REPORT zlc_sliding_window.
7+
*we have a 3 characters a,b, c and we have a text abcdbcacdbac
8+
*find the number of combinations a b c in text
9+
*like here index 1 - abc, index 5 bca, index 10 bac , write a abap program for this
10+
11+
12+
PARAMETERS: p_text TYPE string DEFAULT 'abcdbcacdbac'.
13+
14+
DATA: lv_count TYPE i VALUE 0,
15+
lv_substr TYPE string,
16+
lv_len TYPE i.
17+
18+
lv_len = strlen( p_text ).
19+
20+
DO lv_len - 2 TIMES.
21+
DATA(lv_idx) = sy-index - 1.
22+
lv_substr = p_text+lv_idx(3). " Take 3-character substring
23+
24+
IF lv_substr CS 'a' AND lv_substr CS 'b' AND lv_substr CS 'c'.
25+
lv_count = lv_count + 1.
26+
WRITE: / 'Match at index', sy-index, ':', lv_substr.
27+
ENDIF.
28+
ENDDO.
29+
30+
WRITE: / 'Total combinations of a, b, c:', lv_count.
31+
32+
33+
34+
35+
*
36+
*PARAMETERS: p_text TYPE string DEFAULT 'abcdbcacdbac'.
37+
*
38+
*DATA: lv_len TYPE i,
39+
* lv_pos TYPE i,
40+
* lv_count TYPE i VALUE 0,
41+
* lv_char1 TYPE c LENGTH 1,
42+
* lv_char2 TYPE c LENGTH 1,
43+
* lv_char3 TYPE c LENGTH 1,
44+
* lv_triplet TYPE string.
45+
*
46+
*lv_len = strlen( p_text ).
47+
*
48+
*DO lv_len - 2 TIMES.
49+
* lv_pos = sy-index - 1.
50+
*
51+
* " Access each character by index
52+
* lv_char1 = p_text+lv_pos(1).
53+
* lv_char2 = p_text+lv_pos+1(1).
54+
* lv_char3 = p_text+lv_pos+2(1).
55+
*
56+
* " Count presence of a, b, c manually
57+
* DATA(na) = 0.
58+
* DATA(nb) = 0.
59+
* DATA(nc) = 0.
60+
*
61+
* LOOP AT VALUE #( lv_char1 lv_char2 lv_char3 ) INTO DATA(ch).
62+
* IF ch = 'a'.
63+
* na = na + 1.
64+
* ELSEIF ch = 'b'.
65+
* nb = nb + 1.
66+
* ELSEIF ch = 'c'.
67+
* nc = nc + 1.
68+
* ENDIF.
69+
* ENDLOOP.
70+
*
71+
* IF na = 1 AND nb = 1 AND nc = 1.
72+
* lv_count += 1.
73+
* WRITE: / 'Match at index', sy-index, '→', lv_char1, lv_char2, lv_char3.
74+
* ENDIF.
75+
*
76+
*ENDDO.
77+
*
78+
*WRITE: / 'Total combinations of a, b, c:', lv_count.

src/zlc_sliding_window.prog.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_PROG" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<PROGDIR>
6+
<NAME>ZLC_SLIDING_WINDOW</NAME>
7+
<SUBC>1</SUBC>
8+
<RLOAD>E</RLOAD>
9+
<FIXPT>X</FIXPT>
10+
<UCCHECK>X</UCCHECK>
11+
</PROGDIR>
12+
<TPOOL>
13+
<item>
14+
<ID>R</ID>
15+
<ENTRY>Program ZLC_SLIDING_WINDOW</ENTRY>
16+
<LENGTH>26</LENGTH>
17+
</item>
18+
</TPOOL>
19+
</asx:values>
20+
</asx:abap>
21+
</abapGit>

0 commit comments

Comments
 (0)