0% found this document useful (0 votes)
8 views

LeetcodeDay 22

Uploaded by

dhruuvnaik5702
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

LeetcodeDay 22

Uploaded by

dhruuvnaik5702
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

GDSC-DBIT DSA Leetcode 45 (Day-22)

Question 1: 1816. Truncate Sentence


Tags: Array String

 PseudoCode:-

Approach 1:

Function truncateSentence(s: String, k: Integer) -> String:


// Split the input string into an array of words
wordsArray = s.split(" ")

// Initialize an empty StringBuilder to store the truncated sentence


truncatedSentence = StringBuilder()

// Iterate through the first k words and append them to the StringBuilder
for i = 0 to k-1:
truncatedSentence.append(wordsArray[i]).append(" ")

// Convert the StringBuilder to a string and trim any extra spaces


result = truncatedSentence.toString().trim()

// Return the truncated sentence


return result

Time Complexity: O(n+k)


Space Complexity: O(n+k)

Approach 2:

Function truncateSentence(s: String, k: Integer) -> String:


// Initialize variables to track current index and space count
idx = 0
spaceCount = 0

// Iterate through the string until reaching k spaces or end of the string
while idx < length(s) AND spaceCount < k:
if s[idx] == ' ':
spaceCount++
idx++

// Check if spaceCount is less than k (meaning include the whole string)


if spaceCount < k:
result = s
else:
// Exclude the extra space after the kth word
result = substring(s, 0, idx - 1)

// Return the truncated or full sentence


return result

Time Complexity: O(n)


Space Complexity: O(1)

Question 2: 2103. Rings and Rods


Tags: Hash Table String

 PseudoCode:-

Approach 1:

Function countPoints(rings: String) -> Integer:


// Initialize sets to store the positions of red, green, and blue rings
red = HashSet()
green = HashSet()
blue = HashSet()

// Iterate through the rings string


for i = 0 to rings.length() - 1:
// Check the color of the current ring and add its position to the respective set
if rings.charAt(i) == 'R':
red.add(rings.charAt(i + 1) - '0')
else if rings.charAt(i) == 'B':
blue.add(rings.charAt(i + 1) - '0')
else if rings.charAt(i) == 'G':
green.add(rings.charAt(i + 1) - '0')

// Initialize a variable to count the points


count = 0
// Iterate through positions 0 to 9
for i = 0 to 9:
// Check if the position i is present in all three sets (red, green, and blue)
if red.contains(i) and green.contains(i) and blue.contains(i):
count++

// Return the total count of points


return count

Time Complexity: O(n)


Space Complexity: O(1)

Approach 2:

Function countPoints(rings: String) -> Integer:

// Initialize an array 'rods' with 10 elements, representing each ring position

rods = array of integers with size 10

// Iterate through the rings string with a step of 2

for i = 0 to length of rings - 1 with a step of 2:

// Update the 'rods' array by turning on the respective bit based on the ring position and
color

rods[rings.charAt(i + 1) - '0'] |= (1 << (rings.charAt(i) - 'A'))

// Count the number of rods with all 3 bits turned on

count = 0

for i in rods:

if i == 131138:

count++

// Return the total count of rods with all 3 bits turned on

return count
- Time Complexity: O(N)

- Space Complexity: O(1)

Question 3: 14. Longest Common Prefix


Tags: String Trie

 PseudoCode:-

Function longestCommonPrefix(strs: Array of String) -> String:


// Check if the input array is empty
if length of strs is 0:
return ""

// Initialize the prefix with the first string in the array


prefix = strs[0]

// Iterate through the array starting from the second string


for i = 1 to length of strs - 1:
// While the current string does not start with the current prefix
while strs[i].indexOf(prefix) != 0:
// Reduce the prefix by one character
prefix = prefix.substring(0, length of prefix - 1)

// Return the final common prefix


return prefix

Time Complexity: O(n )


Space Complexity: O(1)

You might also like