Problem Statement: You are given a string str of length n. Your task is to find and print the maximum integer present in the string str. An integer is defined as a sequence of digits.
Note:
Print -1 if no integer is found.
If the string is empty, print -2.
Input Format: The input consists of two lines:
The first line contains an integer, i.e., n.
The second line contains the string str.
Output Format: Print the maximum integer present in the string str.
Question 2: Product Sentiment
Problem Statement: You are given a set of customer reviews for a specific product. Each review contains feedback from a customer, with certain words that indicate positive or negative sentiment. Your task is to analyze all the reviews collectively to classify the overall sentiment for the product. Based on this classification, the product will be labeled as either a Good Product or a Bad Product.
A review of the product will be Positive if it contains MORE positive words than negative words, otherwise Negative.
Overall sentiment of the product will be Positive if it has MORE positive reviews than negative reviews, otherwise Negative.
Write a function that returns "Good Product" or "Bad Product".
We need to extract and evaluate all contiguous sequences of numerical digits embedded within the provided string.
By iterating through the characters, we can build numbers dynamically and update our maximum value whenever we hit a non-digit character.
Special flags easily handle the edge cases of an empty string or a string completely devoid of numbers.
Approach
1. Handling Edge Cases First
Before we do any work, we check if the string length n is 0 or if the string is completely empty. If so, we immediately return -2 as per the problem's strict instructions.
2. Building and Comparing Numbers
We iterate through the string character by character. We use a built-in function like isdigit() to check if the current character is a number. If it is, we multiply our current running total by 10 and add the new digit. When we finally hit a non-digit character (like a letter or space), it means our number has ended. We compare it against our tracked maximum, update the maximum if necessary, and reset our running total back to 0 for the next number.
Time Complexity
Time: O(N) - We traverse the length of the string exactly once.
Space: O(1) - We only use a few integer variables and boolean flags to keep track of our state.
Code Implementation
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
long long maxIntegerInString(int n, string str) {
// Edge case: Empty string
if (n == 0 || str.empty()) {
return -2;
}
long long maxNum = -1;
long long currentNum = 0;
bool inNumber = false;
bool foundAny = false;
for (int i = 0; i < n; i++) {
if (isdigit(str[i])) {
// Build the integer digit by digit
currentNum = currentNum * 10 + (str[i] - '0');
inNumber = true;
foundAny = true;
} else {
// A non-digit breaks the sequence
if (inNumber) {
maxNum = max(maxNum, currentNum);
currentNum = 0;
inNumber = false;
}
}
}
// Check if the string ended while still building a number
if (inNumber) {
maxNum = max(maxNum, currentNum);
}
// Edge case: No numbers found in the entire string
if (!foundAny) {
return -1;
}
return maxNum;
}
int main() {
// Sample Case
string str = "abc123xyz45def";
cout << maxIntegerInString(str.length(), str) << "\n"; // Output: 123
return 0;
}
We evaluate the sentiment of the product by breaking down each individual review into isolated words and checking them against predefined keyword lists.
A review is deemed positive only if its count of positive keywords strictly exceeds its count of negative keywords.
Finally, the product receives a good rating if the total number of positive reviews strictly outnumbers the negative reviews.
Approach
1. Fast Keyword Lookups
We load the provided positive and negative keywords into two separate Hash Sets. Hash Sets provide O(1) constant time lookups, which is highly efficient when we need to check dozens of words across hundreds of reviews.
2. Parsing and Counting
We loop through each review string and use a stringstream to automatically break the sentence into individual words separated by spaces. For each word, we check if it exists in our positive set or negative set, tallying the results. We use the final tally of that specific review to increment either our global positiveReviews counter or negativeReviews counter, ultimately dictating the final product label.
Time Complexity
Time: O(N * W) - Where N is the number of reviews and W is the average number of words per review. Hash Set lookups take constant time.
Space: O(K + W) - Where K is the total number of keywords stored in the sets, and W is the space used by the stringstream to hold the words.
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <sstream>
using namespace std;
string analyzeProductSentiment(int n, vector<string>& reviews) {
// Step 1: Initialize hash sets for O(1) lookups
unordered_set<string> posWords = {
"good", "excellent", "amazing", "love", "great", "satisfied", "happy"
};
unordered_set<string> negWords = {
"bad", "terrible", "disappointed", "poor", "hate", "unsatisfied", "awful"
};
int positiveReviewsCount = 0;
int negativeReviewsCount = 0;
// Step 2: Process each review individually
for (const string& review : reviews) {
stringstream ss(review);
string word;
int posCount = 0;
int negCount = 0;
// Extract words separated by spaces
while (ss >> word) {
if (posWords.count(word)) {
posCount++;
} else if (negWords.count(word)) {
negCount++;
}
}
// Determine if the single review is positive or negative
if (posCount > negCount) {
positiveReviewsCount++;
} else {
negativeReviewsCount++;
}
}
// Step 3: Determine overall product sentiment
if (positiveReviewsCount > negativeReviewsCount) {
return "Good Product";
} else {
return "Bad Product";
}
}
int main() {
// Sample Case
int t = 1;
int n = 3;
vector<string> reviews = {
"this is a great and amazing product",
"i hate this terrible item",
"love the quality very happy"
};
// Output: Good Product (2 positive reviews, 1 negative review)
cout << analyzeProductSentiment(n, reviews) << "\n";
return 0;
}
Problem1 -
Max Integer in String Solution
Topics Involved / Prerequisites
Overview
We need to extract and evaluate all contiguous sequences of numerical digits embedded within the provided string.
By iterating through the characters, we can build numbers dynamically and update our maximum value whenever we hit a non-digit character.
Special flags easily handle the edge cases of an empty string or a string completely devoid of numbers.
Approach
1. Handling Edge Cases First
Before we do any work, we check if the string length n is 0 or if the string is completely empty. If so, we immediately return -2 as per the problem's strict instructions.
2. Building and Comparing Numbers
We iterate through the string character by character. We use a built-in function like isdigit() to check if the current character is a number. If it is, we multiply our current running total by 10 and add the new digit. When we finally hit a non-digit character (like a letter or space), it means our number has ended. We compare it against our tracked maximum, update the maximum if necessary, and reset our running total back to 0 for the next number.
Time Complexity
Code Implementation
Problem2 -> Product Sentiment Solution
Topics Involved / Prerequisites
Overview
We evaluate the sentiment of the product by breaking down each individual review into isolated words and checking them against predefined keyword lists.
A review is deemed positive only if its count of positive keywords strictly exceeds its count of negative keywords.
Finally, the product receives a good rating if the total number of positive reviews strictly outnumbers the negative reviews.
Approach
1. Fast Keyword Lookups
We load the provided positive and negative keywords into two separate Hash Sets. Hash Sets provide O(1) constant time lookups, which is highly efficient when we need to check dozens of words across hundreds of reviews.
2. Parsing and Counting
We loop through each review string and use a stringstream to automatically break the sentence into individual words separated by spaces. For each word, we check if it exists in our positive set or negative set, tallying the results. We use the final tally of that specific review to increment either our global positiveReviews counter or negativeReviews counter, ultimately dictating the final product label.
Time Complexity